up vote 0 down vote favorite

Hello,

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

link|flag
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 – Pekka Sep 14 at 12:43
Why don't you use AJAX? – fabrik Sep 14 at 12:44
Or document.cookie ? – gnarf Sep 14 at 12:46
why don you send the variable into a hidden field and access the field with $_GET or $_POST ? – luvboy Sep 14 at 12:47
how to use document.cookie? WIll that do? – Shampa Sep 14 at 12:47

5 Answers

up vote 1 down vote

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.

link|flag
up vote 0 down vote

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.

link|flag
1  
Sorry, as PHP runs, then sends the page, then JavaScript runs, this won't work as expected. – Delan Azabani Sep 14 at 12:50
up vote 0 down vote

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

link|flag
up vote 0 down vote

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

link|flag
well..seems like I have left with no options. – Shampa Sep 14 at 13:32
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. – d2burke Sep 14 at 13:56
up vote 0 down vote

There are a number of ways to achieve something close to what you want, but not like that.

Javascript and PHP run in different contexts, and your programs in each don't know anything about each other. You can't use a PHP variable in Javascript the way you've asked, nor vice-versa.

You need to know the way the web page works:

  • The user requests the page
  • The web server runs the PHP code to generate a complete HTML document.
  • The web server sends the HTML document to the browser.
  • The browser opens the HTML document and runs any javascript within it.

As you can see from that, the PHP is run first, before any JS code, regardless of the order they appear in your source code file.

link|flag

Your Answer

get an OpenID
or
never shown

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