acmumph Posted April 2, 2013 Share Posted April 2, 2013 So I want to pass a php variable to the following line of code: echo '<script type="text/javascript">alert("Registration number already exist");history.back(); </script>'; I thought I could do this: $regnum="12345"; echo '<script type="text/javascript"> var regnum="<?php echo $regnum; ?>";alert("Registration number:"+regnum+" already exist");history.back(); </script>'; The alert displays "Registration number:<?php echo regnum;?> already exist"... Appreciate any feedback. Quote Link to comment https://forums.phpfreaks.com/topic/276409-php-variable-to-javascript/ Share on other sites More sharing options...
Solution kicken Posted April 2, 2013 Solution Share Posted April 2, 2013 echo '<script type="text/javascript">alert("Registration number: '.$regnum.' already exists");history.back();</script>';Just concatenate it into the output string like you would any other variable. The fact that it is going into a JS string doesn't really make that much of a difference. Quote Link to comment https://forums.phpfreaks.com/topic/276409-php-variable-to-javascript/#findComment-1422393 Share on other sites More sharing options...
acmumph Posted April 2, 2013 Author Share Posted April 2, 2013 For some reason I couldn't seem to get that right. Something about single and double quotes screw me up.... Appreciate the solution.. Quote Link to comment https://forums.phpfreaks.com/topic/276409-php-variable-to-javascript/#findComment-1422546 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.