Javascript variable in PHP-Need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mathewvp
Forum Commoner
Posts: 28
Joined: Wed Apr 23, 2003 10:28 am

Javascript variable in PHP-Need help

Post 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.
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post 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
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post 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()
&#123;
     document.getElementById("val").value = a;
&#125;
</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.
mathewvp
Forum Commoner
Posts: 28
Joined: Wed Apr 23, 2003 10:28 am

Exact Requirement

Post 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");
?>
}
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
mathewvp
Forum Commoner
Posts: 28
Joined: Wed Apr 23, 2003 10:28 am

Thanks werlop

Post by mathewvp »

That was a wonderful piece of code.Thanks very much.It helped.
Post Reply