Send value to javascript function

0

Hello

I'm trying to send a value to a function by clicking the button, what could be wrong?

echo"<button id=".$nome." onclick='produtoComprado(/'".$detalhes."/')'>".$nome."</button>";

Thank you

    
asked by anonymous 08.10.2017 / 17:25

2 answers

1

When you use double quotation marks " , there is no need to do concatenation in this way, you can just place the variable, and if you need to put quotation marks inside the text, just use the backslash before \" to escape, the code gets cleaner and makes life easier.

echo "<button id=\"$nome\" onclick=\"produtoComprado('$detalhes')\">$nome</button>";

Follow the example by running: link

To learn more about the difference in quotation marks: Difference between single and double quotation marks in PHP

    
08.10.2017 / 17:43
1

It was missing two simple quotes when setting the button id, test:

echo "<button id='".$nome."' onclick='produtoComprado(/'".$detalhes."/')'>".$nome."</button>";
    
08.10.2017 / 17:29