tags:

views:

81

answers:

5

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

A: 

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.

Fincha
Sorry, as PHP runs, then sends the page, then JavaScript runs, this won't work as expected.
Delan Azabani
+1  A: 

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.

Delan Azabani
A: 

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

Dick
A: 

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

jaram
well..seems like I have left with no options.
Shampa
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
A: 

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.

Spudley