simedogz Posted April 16, 2011 Share Posted April 16, 2011 hello all, I inserted a javascript event handler 'onclick' into php code. the onclick has a php variable in it that is supposed to trigger an alert when clicked. I can't figure out how to escape the php variable correctly, so I just left it at the last thing I tried, which I know is wrong. Any help is appreciated. thanks <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> function popUp(a) { alert(a); } </script> </head> <body> <?php $gmail ='yaaah'; echo '<p onclick=popUp(\'$gmail\')>lkjlkjlkjlkjlkjlkj</p>'; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/233867-using-an-onclick-in-php/ Share on other sites More sharing options...
lastkarrde Posted April 16, 2011 Share Posted April 16, 2011 echo '<p onclick="popUp(\'$gmail\')">lkjlkjlkjlkjlkjlkj</p>'; Quote Link to comment https://forums.phpfreaks.com/topic/233867-using-an-onclick-in-php/#findComment-1202223 Share on other sites More sharing options...
cyberRobot Posted April 16, 2011 Share Posted April 16, 2011 echo '<p onclick="popUp(\'$gmail\')">lkjlkjlkjlkjlkjlkj</p>'; Variables won't work in single-quoted strings. You'll need to change it to: echo "<p onclick=\"popUp('$gmail')\">lkjlkjlkjlkjlkjlkj</p>"; Quote Link to comment https://forums.phpfreaks.com/topic/233867-using-an-onclick-in-php/#findComment-1202232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.