i need help with this PHP script that doesn’t works for some reason as below
<?php
.
.
.
echo "<li><a href='#' onclick='print(); return false; _gaq.push(['_trackEvent', 'check', 'test1', 'Print01']); '>";
echo "</a></li>";
.
.
.
?>
if i remove the google tracking event code onclick print() will work without any problem,
does anybody know where is the problem or may be i miss something ?
It seems you’re confusing PHP and Javascript code. The “print()” in the onclick handler is Javascript, and unless you have a Javascript function defined that is named
print, it’ll fail and the link will be loaded. There’s also no use in having the analytics call after returning false, since it never will run.Edit: I misred a bit. You’re using ‘ in the code, as well as using ‘ to delimit the text contained. You’d probably be better off by creating a separate function to handle the click event, or you’ll have to escape the single quotes.
.. but remember that PHP will also have escape codes, and you might have to escape the \ as well. I’d rather move it out to a separate function:
Allowing ‘check’, ‘test1’, ‘Print01’ to be provided as arguments to handleClick will also make reuse easier.