Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1367
  • Last Modified:

pass javascript variable to php

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.

0
liquidblaze
Asked:
liquidblaze
  • 3
  • 3
  • 2
  • +2
1 Solution
 
venkateshwarrCommented:

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.

venkat.
0
 
liquidblazeAuthor Commented:
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 :)
0
 
ZvonkoSystems architectCommented:
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.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

<html>
<head>
</head>
<body>

<script language="Javascript">
<!--

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

-->
</script>

</body>
</html>

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!
0
 
gam3r_3xtr3m3Commented:
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;}}
else{hiddenForm.hiddenField.value=clientName;hiddenForm.submit();}}
</script>
<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="" />
</form>

then in the newpage.php:

<?php
if(empty($_POST["hiddenField"])){$client_name=$_POST["showField"];}else{$client_name=$_POST["hiddenField"];}
if(empty($client_name)){print("No client name was declared.");}else{print("Welcome $client_name");}
?>
0
 
mrooneyafdoCommented:
Gam3r, that seems kind of unnecessary and overboard for what he wants. But maybe I am wrong.
0
 
liquidblazeAuthor Commented:
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?
0
 
mrooneyafdoCommented:
Liquidblaze, have you tried mine? It uses a different method to redirect, and also seems slightly less complicated to me.
0
 
gam3r_3xtr3m3Commented:
try this:

<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;}}
else{document.hiddenForm.hiddenField.value=clientName;document.hiddenForm.submit();}}
</script>
<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="" />
</form>

it works on my Mozilla Firefox. =p

gam3r
0
 
liquidblazeAuthor Commented:
thx gam3r - outstanding job.

sorry for only being able to give you 40 points. I wish I had more :)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now