Page 1 of 1
Javascript variable in PHP-Need help
Posted: Thu May 22, 2003 9:30 am
by mathewvp
Hi,
How do I take the value stored in a javscript variable into a php variable.for eg,var a=10; in javscript.I want to store this value in $val.
Posted: Thu May 22, 2003 9:37 am
by werlop
I had the same problem, what I did was make a string containing all the variables I wanted like so:
Code: Select all
<script language="JavaScript">
w=screen.width;
h=screen.height;
n=navigator.appName
if (n != "Netscape") {c=screen.colorDepth}
else {c=screen.pixelDepth}
a="<?php print $REQUEST_URI; ?>";
j=navigator.javaEnabled();
imgquerystring = "width="+w+"&height="+h+"&navigator="+n+"&action="+a+"&java="+j+"&colours="+c+"";
getimgstring = "<img src='include/add.php?"+imgquerystring+"' width='10' height='1' border='0'>";
</script>
Then I requested an image:
Code: Select all
<script language="JavaScript">
document.write(getimgstring);
</script>
The image is actually a PHP file, and the variables are processed by using the $_GET[] array. The PHP file just contains code to process the variables, and output a fake image:
Code: Select all
<?php
header("Content-type: image/gif");
$file = "admin/images/leeg.gif";
$data = fread(fopen($file, "r"), filesize($file));
echo $data;
?>
Hope this helps,
David
Posted: Thu May 22, 2003 12:07 pm
by liljester
you have to do a form submit... make a hidden form element, and use JS to set the value of the element. then submit the form.
ive used something like this in the past:
Code: Select all
<html>
<head>
<script language="javascript">
a = 10;
function change_val()
{
document.getElementById("val").value = a;
}
</script>
</head>
<body onload="chang_val();">
<form action="whatever.php" method="POST">
<input type="hidden" name="val" ID="val">
<input type="submit" name="action" value="submit">
</form>
</body>
<html>
hope that helps.
Exact Requirement
Posted: Fri May 23, 2003 12:51 am
by mathewvp
This is what I want to do.Note that the form doesn't get submitted.The "somefunction()" is invoked when the user selects an option from a combobox.
function somefunction()
{
var a=document.form1.city.value;
<?
$val=a;(HERE I WANT TO STORE THE JAVASCRIPT VARIABLE);
mysql_query("SELECT * FROM place WHERE city=$val");
?>
}
Posted: Fri May 23, 2003 1:03 am
by scorphus
You just can't do this. All JavaScript code is run on client's side, *after* all server operations including PHP processing phase.
So you have to send to the server any information you want from the user/session to be processed by PHP. And both of the above solutions are fine. Also you can submit you form via JavaScript.
Hope it helps. Cheers,
Scorphus.
Thanks werlop
Posted: Sat May 24, 2003 10:02 am
by mathewvp
That was a wonderful piece of code.Thanks very much.It helped.