Jump to content

PHP Variable to javascript


Go to solution Solved by kicken,

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

  • Solution

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.