JavaScript function from PHP

I have a JavaScript function pop_item

. I need to call this from PHP, so my PHP code looks like this:

echo '<a href="javascript:pop_item('.$_code.',1)">Link </a>';

      

It throws no errors but pop_item

doesn't work,

HTML output for the above:

<a href="javascript:pop_item('ABC',1)">Link </a>

      

0


source to share


4 answers


I think the problem is in the pop_item function, since the call seems to be correct. Try the following:

echo " <a href='#' onclick=\"pop_item(".$_code."', 1)\">link</a>";

      

or



echo '<a href="javascript:alert('.$_code.')">Link</a>';

      

See if this works.

+5


source


If your pop_item function takes a string as its first parameter, it may be due to missing some of the quotation marks. Use PHP's interpolation function, so you can be sure which quote is. Something like this line:

 echo "<a href=\"javascript: pop_item('$_code',1);\">Link</a>";

      



If pop_item accepts some other data type, then single quotes are useless. I also recommend using the browser's JavaScript error console to see what the details of the problem are.

+2


source


Your function is probably undefined ... Make sure you include it somewhere ...

0


source


If the result looks correct, then PHP did its job correctly, and the problem is in your JavaScript code. Try running the Firebug page or some other JavaScript debugger to find the problem.

0


source







All Articles