Jump to content

PHP Variable to javascript


acmumph

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/276409-php-variable-to-javascript/
Share on other sites


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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.