Click to See Complete Forum and Search --> : JScript Value to PHP


Rilt
09-03-2003, 06:32 AM
Hi! How do I move a JScript Value into a PHP Variable? Can somebody help?

pyro
09-03-2003, 07:00 AM
Either submit a form, and read it via $_GET or $_POST (or $HTTP_GET_VARS and $HTTP_POST_VARS in PHP < 4.1.0) depending on the method you used for the form, or refresh the page with a query string attached and read it with $_GET...

Rilt
09-03-2003, 07:12 AM
Sorry but Im really new in JScripts. I have to ask you another question:

What do you mean by "or refresh the page with a query string attached and read it with $_GET... "? How should I do that?

pyro
09-03-2003, 07:31 AM
First of all, do you mean JScript or JavaScript? I'm going to assume JavaScript as that is what I know, and that is much more common.

To get the value 10 into your PHP script, you could do it like this:

<script type="text/javascript">
var = 10; //variable to send...
window.location.href = "somepage.php?var="+var;
</script>

And then in the PHP script:

<?PHP
$var= $_GET[var];
echo $var; #will echo 10
?>

Rilt
09-03-2003, 08:42 AM
Hello pyro,

thanks for the quick reply.

I've tried your suggestion - but its not really what Im planning to achieve. Upon activating the script, it only jumps into the next page - 'somepage.php' (for example). I wanted that the value from the Javascript Variable would only be passed once I clicked on the submit button. Is that still possible?

Sorry for asking too much...

pyro
09-03-2003, 12:13 PM
Yes, you can use either the POST or GET method with the form, and access the variables with $_POST or $_GET as I said earlier. I would use POST as it is more secure.

For example:

<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post" name="myform">
<input type="text" name="field1" value="somevalue">
<input type="submit" name="submit" value="Submit">
</form>

and then this bit of PHP to get the value:

<?PHP
$field1 = $_POST['field1'];
?>

and, you can use javascript to set the form fields value...

<script type="text/javascript">
function setVals() {
document.myform.field1.value = "a value set with JavaScript";
}
window.onload = setVals;
</script>

Rilt
09-05-2003, 08:54 AM
Hey, I got it! I got my program running the way I want it to!!!

A thousand thanks to you, pyro!!!! :) :) :)

pyro
09-05-2003, 10:15 AM
Happy to help... :)