We help IT Professionals succeed at work.

pass javascript variable to php

liquidblaze asked
Medium Priority
Last Modified: 2008-02-01
I am looking for a way to pass a javascript variable (which was entered in an alert box) to a php script.

the plan is to enter a contact's name into a popup box and this name is then used to send out a private message.

Watch Question

Javascript is client side and php is server side code.. so you cannot directly assign a javascript varialbe to php...
But you can create a hidden variable and submit it to php.



but the client side should be able to send something back to the script by doing a refresh or something like that. I dont know :)
ZvonkoSystems architect
Top Expert 2006

You can of course send values to php, but you have to store that value in some storage for the next page output.
Therefore it would be best to have a hidden field on the form for that user input.
You can prompt the user by the statement:
userInput = window.prompt("Message:", "default");  

and store the returnded user value in the hidden form field and submit the form to php.

If you do not want to submit the form and use database to handle user input and page output, then you can create a separate php script to handle the user input.
You fetch the user input like upper example and call the php side by a trick: you call a image url as php script.
Like this:

userInput = window.prompt("Message:", "default");  
newImg = new Image();
newImg.src = "/your/script.php?usrInp="+userInput;

On php side does your script.php fetch the usrInp var and store in database for later use.

What you probably want is to have an HTML page that pops up that box.


<script language="Javascript">

nameVar = prompt("Your name:");
url = "yourpage.php?name=" + nameVar;



That would be your HTML page, which then goes to 'yourpage.php' (of course change that to the full address of your PHP page) with the variable name.

Now in PHP, to get that all you need to is (in your PHP page):

$Name = $_GET["name"];

Now you can use the $Name variable however you like!
better to use POST method and forms:

<script language="JavaScript" type="text/javascript">
function showInput(){document.getElementById("showInputBox").style.display="block";}
function doPrompt(){var clientName=window.prompt("Client name:","");
if((clientName==null)||(clientName=="")){alert("You didn't enter any name!");
if(confirm("Re-enter name?")){doPrompt();return false;}else{showInput(); return false;}}
<body onLoad="doPrompt();">
<form name="hiddenForm" method="post" action="newpage.php">
<div id="showInputBox" style="display: none;">Client name:
<input type="text" name="showField" value="" />
<input type="submit" /></div>
<input type="hidden" name="hiddenField" value="" />

then in the newpage.php:

if(empty($client_name)){print("No client name was declared.");}else{print("Welcome $client_name");}
Gam3r, that seems kind of unnecessary and overboard for what he wants. But maybe I am wrong.


gam3r - your script looks like the thing I need

however, my browser (mozilla) doesnt redirect me to newpage.php when I enter the name in the popup box - any suggestions?
Liquidblaze, have you tried mine? It uses a different method to redirect, and also seems slightly less complicated to me.
Unlock this solution and get a sample of our free trial.
(No credit card required)


thx gam3r - outstanding job.

sorry for only being able to give you 40 points. I wish I had more :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.