How to assign ajax response to php session variable..?

How to assign ajax response to php session variable…

Below is my ajax code

        url : "info/session.php",
        type: 'GET',
                          $_SESSION['sid'] = 	data1; //assign ajax response to session variable
              console.log("Data is empty");
        error: function(xhr) {
            alert('Error!  Status = ' + xhr.status + " Message = " + xhr.statusText);

You can’t assign javascript variable value to php variable, but you can assign php variable value to javascript variable. You will have to pass it through the url or pass it using ajax to some file and assign the javascript value to php session in that file.

Also i see that variable data1 is a ajax success response, which means it is already in the php file session.php, so why don’t you just assign that response in session in session.php file??

Hope this helps.

1 Like

makes no sense to me. you already have all data in the script, there you can assign the session data.

You have to remember that this section of code:

$_SESSION['sid'] = 	data1; //assign ajax response to session variable

will execute when the page is first drawn, and not when the Ajax call returns. PHP is only processed on the server, not on the client side. By the time your Ajax call runs and returns, the PHP processing has already finished.

That seems to be the way, set it in the php file that you call via Ajax.

If you’ve used PHP in your main page to recover the value of the session variable, it won’t automatically change, for the same reason - the main PHP is only processed when the page is first drawn.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.