Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pass javascript variable to php

Posted on 2004-03-24
10
Medium Priority
?
1,366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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 160 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

705 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