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
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 to share