Solved

pass javascript variable to php

Posted on 2004-03-24
10
1,359 Views
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.

0
Comment
Question by:liquidblaze
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10668124

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
 
LVL 2

Author Comment

by:liquidblaze
ID: 10668445
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 10669105
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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Expert Comment

by:mrooneyafdo
ID: 10672467
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
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 10672676
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
 

Expert Comment

by:mrooneyafdo
ID: 10673150
Gam3r, that seems kind of unnecessary and overboard for what he wants. But maybe I am wrong.
0
 
LVL 2

Author Comment

by:liquidblaze
ID: 10677534
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
 

Expert Comment

by:mrooneyafdo
ID: 10681010
Liquidblaze, have you tried mine? It uses a different method to redirect, and also seems slightly less complicated to me.
0
 
LVL 7

Accepted Solution

by:
gam3r_3xtr3m3 earned 40 total points
ID: 10683938
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
 
LVL 2

Author Comment

by:liquidblaze
ID: 10687270
thx gam3r - outstanding job.

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

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question