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.
Javascript variable in PHP-Need help
Moderator: General Moderators
I had the same problem, what I did was make a string containing all the variables I wanted like so:
Then I requested an image:
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:
Hope this helps,
David
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>Code: Select all
<script language="JavaScript">
document.write(getimgstring);
</script>Code: Select all
<?php
header("Content-type: image/gif");
$file = "admin/images/leeg.gif";
$data = fread(fopen($file, "r"), filesize($file));
echo $data;
?>David
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:
hope that helps.
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>Exact Requirement
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");
?>
}
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");
?>
}
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
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.
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
That was a wonderful piece of code.Thanks very much.It helped.