-1

Possible Duplicate:
javascript variable into php

Okay lets imagine I've a javascript variable "x"

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

Now I want its value in php variable $y <?php $y; ?> How to do it.

Okay few people are confused i wanted to know is it possible if yes then how ?? if this isn't possible then comment here i'll remove it.

7
  • 1
    in PHP how? are you passing it to PHP? Commented Oct 12, 2011 at 16:52
  • 3
    That variable exists in the browser, a php variable exists on the server, two very separate places. can you please elaborate on what your use is and we can suggest a method? The first thing that comes to mind is to submit a form that sends that value to the server, but, again, it depends on what you want Commented Oct 12, 2011 at 16:53
  • your options are ajax, POST, GET, href which one you want Commented Oct 12, 2011 at 16:57
  • 3
    Someone needs to understand, that when javascript ir executed, the server has done it's job - there is no way to get back to it [PHP] in the current request. You have to make another request, perhaps, via ajax. Commented Oct 12, 2011 at 16:57
  • @Briedis Thanks got my answer. Commented Oct 12, 2011 at 17:00

1 Answer 1

1

You have to pass it to your PHP script, either via AJAX or via a formular with a submit button.

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){}; // change
xhr.open("POST", "your-script.php");
xhr.send("x="+x);

And the PHP script can look like this:

<?php

$y = isset($_POST['x']) ? $_POST['x'] : '';
echo $y;

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

5 Comments

is it possible with simple javascript or not ??
Yes! Look at the first code - this is (simple) JavaScript.
ok can i ask one more question sorry i'm php developer but really bad in js what XMLHttpRequest(); can do ???
You can send requests to other scripts and servers with that object. This is also called AJAX (Asynchronous JavaScript And XML). Here are probably more information: developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest
okay thanks dude my mind is too much clear than before now with js thanks for help thumbs up

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.