0

I want to send two php values to a javascript function. Apparently it doesn't do anything. Is my syntax correct?

<script>
function onmouseclick(nr, name){
        alert(nr);
        alert(name);
    }
</script>

<?php
echo '<img onClick="javascript: onmouseclick('. $template->id .','. $template->name .');" "class="img" />';
?>
1
  • 1
    I encourage you to read the articles on quirksmode.org to learn about other (better) ways to bind event handlers. Then you can avoid all this string concatenation stuff: quirksmode.org/js/introevents.html. It would already help if you'd embed PHP into HTML/JS, not the other way round. Commented Feb 21, 2014 at 10:53

4 Answers 4

5

You should add quotes:

echo '<img onClick="javascript: onmouseclick("'. $template->id .'", "'. $template->name .'");"
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure if this works but the browser will render the output as <img onClick="javascript: onmouseclick("aa", "bb"); /><script> here aa and bb are some values and so echo '<img onClick="javascript: onmouseclick(\''. $template->id .'\',\''. $template->id .'\');" class="img" />'; may solve ?
I use onClick="javascript: onmouseclick('.$template->id.','. $template->name .');" The syntaxis is correct. The problem is in the $template->name
1

Yes you need to use as

echo '<img onClick="javascript: onmouseclick(\''. $template->id .'\',\''. $template->id .'\');" class="img" />';

Need to add the param inside quotes ' ' but since you are doing echo '...' you need to use escape \ so that the quotes are treated as string.

Comments

0

just use without php tags

<img onClick="javascript:onmouseclick('<?php echo $template->id;?>','<?php echo $template->name;?>');" class="img" />

or with php tags

echo '<img onClick="javascript: onmouseclick("'. $template->id .'", "'. $template->name .'");"

2 Comments

This is a possibility but it's not what i want because i want to put the echo '<img.... in a while loop.
@Julian: You still don't need echo for that. In fact, you should avoid echoing HTML as much as possible. It just makes your code harder to maintain in the long run. Here is an example with a while loop and without echoing the HTML: codepad.org/B6Y0eeJZ. If you are using a fairly recent version of PHP, you can even use <?= instead of <?php echo, which makes the code even more readable.
0

you can use this for avoiding character escape

echo sprintf('<img onclick="%s" class="img"/>',sprintf("onmouseclick(%d,'%s');",$template->id,$template->id)); 

1 Comment

echo sprintf() is an "antipattern". There is absolutely no reason that anyone should ever write echo sprintf() in any code for any reason -- it should be printf() without echo every time. This answer should have one printf(), no echo and no sprintf() calls. If you are going to keep this answer here, please edit it to completely refactor the approach.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.