<button class="addToPlaylist" onclick="javascript:myPopup(<?php echo $videos[$counter]?>);
return false;">+</button>

I have a button on an image as a html hyperlink. I want to perform different actions on hyperlink and button. The above code works whenever I do not pass the PHP variable using echo. When i pass PHP variable, the button also performs the same action as of the hyperlink, that means return false does not work.

Any idea why the return false; does not work when i pass PHP variable?

share|improve this question
2  
Check the HTML source, see what PHP has generated. – Xeon06 Jan 16 '12 at 17:11
1  
Is $videos[$counter] a string? If so, you need quotes around it... – Brad Jan 16 '12 at 17:12
feedback

3 Answers

up vote 2 down vote accepted

This should be:

<button class="addToPlaylist" onclick="javascript:myPopup('<?php echo $videos[$counter];?>');return false;">+</button>

Note the single quotes in myPopup. As you pass a string to myPopup, you will need to enclose it with single quotes. (Double won't work as there is already double quotes for the onclick)

share|improve this answer
feedback

I am quite sure $videos[$counter] is not numeric, but a string. In this case you have to write the quotes:

onclick="javascript:myPopup('<?php echo $videos[$counter]?>');

And make sure, $videos[$counter] doesn't contain any, something like

onclick="javascript:myPopup('<?php echo addslashes($videos[$counter])?>');

comes to mind.

share|improve this answer
Thanks. It worked! – user1127223 Jan 16 '12 at 18:15
feedback

onclick="javascript:myPopup("";return false;" . This should work and i think it's more clear where you have javascript code and php code.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.