4

Possible Duplicate:
How to pass JavaScript variables to PHP?

I want to assign the javascript variable to php variable

$msg = "<script>document.write(message)</script>";
$f = new FacebookPost;
$f->message  = $msg;

But it is not working......

3
  • 1
    I believe if you explain the situation a little more we can surely help. Commented Apr 18, 2012 at 11:00
  • message is a javascript variable containing some value. I want to assign this value in php variable.. – Commented Apr 18, 2012 at 11:09
  • It seems like you're misunderstanding. PHP runs on the server, Javascript runs on the client. Commented Oct 4, 2012 at 5:36

4 Answers 4

6

PHP is executed before javascript. You can't do that. PHP is server based, JavaScript is client based.

When PHP is finished executing, it outputs HTML. In the HTML there can be JavaScript code also. So PHP can 'control' JavaScript by outputting JavaScript code to the page, but not the other way around. You'd have to use Ajax-call for that.

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

Comments

5

Javascript works on the user's computer, PHP works on your server. In order to send variables from JS to PHP and vice versa, you need communication such as a page load.

Have a look here on what you can do: How to pass JavaScript variables to PHP? or more specifically the first answer (https://stackoverflow.com/a/1917626/1311593)

Comments

1

Javascript, is a client side script, Where as PHP is a server side script.

The only way you can send data to PHP is through redirection or ajax.

Comments

0

You cannot do that this way. You have to transmit values via $_GET, $_POST or $_COOKIE, after the script has been executed by client.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.