Solved

pass javascript variable to php

Posted on 2004-03-24
10
1,357 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
 

Expert Comment

by:mrooneyafdo
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Expert Comment

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

Author Comment

by:liquidblaze
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thx gam3r - outstanding job.

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now