Craig Harkins Craig Harkins - 12 days ago 6
HTML Question

echoing a functional in php

I'm trying to echo a functional button in php.

echo "<button type='button' onclick="alert('Hello world!')" >Buy</button>";


All it is supposed to do is the user presses it and displays "hello world".

Ben Ben
Answer

Change your code to this. Currently you have double speech marks within the code you want echoed. This means it is being mistaken for the end of the code in the middle of your echo statement. Putting backslashes in front of these escapes them so that they are are not interpreted as being part of the PHP syntax.

echo "<button type='button' onclick='alert(\"Hello world!\")' >Buy</button>";