How do I pass PHP parameters through an sprintf() to a javascript function? -


i have been charged taking on project develop web tool/interface proprietary bit of software. current sticking point (using established practices / patterns) perform connection test server based upon values entered user.

currently, there sub-menu formatted sprintf(). have attempted various methods pass parameters, receiving javascript funciton fails.

the submenu:

$submenu = sprintf('<div id="menu"><ul id="controlsubmenu">%s</ul></div>',                 '<li><a href="#inner_content" class="testconn" id="testconn" onclick="testpbxconnection($ct_host, $ct_port, $ct_un, $ct_pw);return false;">test connection</a></li>  <li> <a href="#inner_content" class="editconn" id="editconn" onclick="editpbxconnection();return false;">edit connection</a></li>'); 

the receiving js function, in file:

function testconnection(a, b, c, d) {   alert('test function hit.'); alert('host: ' + a); alert('port: ' + b); alert('user name: ' + c); alert('password:' + d); } 

am on right track? assistance appreciated.

there few problems here, sprintf used variable substitution in strings, being done kind of oddly passing in string variables parameter. aren't quoting javascript variables, cause error. additionally, function names aren't same testpbxconnection vs. testconnection. sprintf call should more like:

$submenu = sprintf('<div id="menu">        <ul id="controlsubmenu">          <li>             <a href="#inner_content" class="testconn" id="testconn" onclick="testconnection(\'%s\', \'%s\', \'%s\', \'%s\');return false;">test connection</a>          </li>          <li>              <a href="#inner_content" class="editconn" id="editconn" onclick="editpbxconnection();return false;">edit connection</a>          </li>        </ul>      </div>', $ct_host, $ct_port, $ct_un, $ct_pw); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -