0

I have a javascript variable which holds some information and I want that to assign in a PHP variable. Here is what I am using:

<script type="text/javascript">
function redirectToFacebook()
{
    var facebookMessage = encodeURI(document.getElementById('txt_msg').value);
}
</script>

<?php
    $_SESSION['sess_facebook_message'] = facebookMessage;
?>

Any help is really appriciable.

Thanks in advance

3
  • 13
    You have a misunderstanding here. Javascript runs in the browser, long after PHP has served the document, so it is not possible to assign variables from JS to PHP like that. You need to explain what you want to do so somebody can suggest a workaround Commented Sep 14, 2010 at 12:43
  • why don you send the variable into a hidden field and access the field with $_GET or $_POST ? Commented Sep 14, 2010 at 12:47
  • how to use document.cookie? WIll that do? Commented Sep 14, 2010 at 12:47

4 Answers 4

2

Because PHP runs on the server, and JavaScript in the client, there is no way to set a PHP session variable after JavaScript works with it, as PHP has done executing before the page was even sent.

However...

If you use JavaScript to make a request (AJAX, imagehack or otherwise) to a PHP script that sets the variable, you can.

For example...

JavaScript:

function something() {
    // do something with somevar
    somevar = 'content';
    // make an AJAX request to setvar.php?value=content
}

PHP:

$_SESSION['somevar'] = $_GET['somevar'];

Make sure you take security issues of client-generated data into account, though.

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

Comments

0

If you want to pass variables from the browser (javascript) to your backend server (PHP), you need to either:

1) Load a new page with Javascript parameters encoded either as POST or GET 2) Asynchronously call a PHP script (AJAX call) encoding the parameters as POST or GET

A simple example using a GET request (you simply append your parameters to the URL):

<script>
    window.location = '/some-url?' + document.getElementById('text_msg').value;
</script>

You probably want to assign this piece of code to a button or something...

Comments

0

what you are trying to achieve is not possible due to API limitation.It does not provide that.

3 Comments

well..seems like I have left with no options.
Shampa...what's your next step with the php variable? Sometimes I set the value of a hidden <input> with javascript which makes it easier to send that value to a PHP script via POST or GET with a form, or via AJAX...just a thought.
I don't see what any API has to do with it.
-1

may be you can try to redirect with javascript and pass variables form php to js. They way yout tru it, it can't work.

may be, im realy not shure try this.

<?php
function redirectToFacebook() { 

var facebookMessage = ?>
<script>
    document.write(encodeURI(document.getElementById('txt_msg').value)); 
</script>
<?php
}
?>

or using cookies.

1 Comment

Sorry, as PHP runs, then sends the page, then JavaScript runs, this won't work as expected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.