The code is like this, however it seems not to be working. Please help

<script type="text/javascript">

var name=prompt("Please enter your name");
</script>
<?php

$_COOKIE['user']=$_GET['name'];
?>
link|improve this question
that is not the way to send var – Shakti Singh Apr 15 '11 at 17:16
3  
Just set a cookie with JS, PHP will be able to see it. – Brad Christie Apr 15 '11 at 17:16
feedback

1 Answer

You are mixing PHP (which runs on server side) with JavaScript (which runs on client side). This is fundamentally impossible. PHP is run long before the JavaScript part.

You probably want to use JavaScript to set the cookie. PHP will be able to read it on the next request.

link|improve this answer
The code that I used above is in fact a .php file. But why is the value in name not being send to the "user" cookie variable. (NB. The cookie has been declared somewhere else.) Please help. – prince Apr 15 '11 at 17:21
2  
@prince because PHP runs on server side. The result of the PHP script is then sent to the client as the HTML file that you see in your browser. JavaScript gets executed only then. It is impossible for JavaScript to interact with PHP in this way. Use JavaScript to set the cookie as shown in the link – Pekka Apr 15 '11 at 17:23
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.