0

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'];
?>
2
  • that is not the way to send var Commented Apr 15, 2011 at 17:16
  • 3
    Just set a cookie with JS, PHP will be able to see it. Commented Apr 15, 2011 at 17:16

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.