How can I get the value of an javascript string and pass it to a php variable?

<script>
    var x = "test";
</script>

<?
    $x = ???? // need to get the value of x
?>
Well as you know, but just as a reminder, when you make a page with both php and javascript in it, the PHP code is executed on the server and then the resulting html plus javascript is send to the user, and the browser executes the javascript.
So it is easy to fill in PHP-made variables into the javascript,
e.g.
<?PHP
$phpvar=3+8+31;
?>

<script language="javascript">
theanswer = <?PHP echo $phpvar; ?>

</script>


But to give javascript results to PHP you have to give it back to another PHP page.

I can think of three ways:
- write a cookie with javascript and have the next page read the cookie with PHP
- set a form element's value with javascript (document.formname.elementname.value=(6*7)) then submit the form (document.formname.submit()), In the following php page read $_POST['elementname'] (PHP>=4.10)
- build the variable into the URL like window.location.href='http://www.mysite.org/nextpage.php?variable1=' + (50-8) +'&variable2='+theanswer . in the nextpage read $_GET['variable1'] and 2


In all cases make sure that you are very careful with inserting the result in SQL queries or to include files. Imagine that [your favourite enemy] tries to add some sql code or include a selfchosen file from the server.






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to