santa
santa

Reputation: 12512

Setting PHP SESSION var from within JS

Is there a way to set a session variable from withing my JS function?

I have the following JS code:

$.ajax({
    url: "ajax.php",
    type: "POST",
    data: { 
        tid: '.$testID.',
        do:"'.$do.'"
    },
    success: function( html ) {
        $("#partBox").html( html ); 
        // add PHP here?            
    }
});

I would like to set the following var in the session, on SUCCESS:

$_SESSION['hgt'] = 'Math.ceil($("#partBox").height() / 2)';

I have no idea if this is even possible... Alternatively I could probably use cookies...

Upvotes: 0

Views: 1715

Answers (1)

John Conde
John Conde

Reputation: 219804

You can't set any PHP variables in JavaScript. Once the Page has finished loading PHP has done its job and is out of the picture. If you want to set a session variable using JavaScript you can do it one of two ways:

  1. Use Ajax to send the value to a PHP script to set the session variable
  2. Store it in a cookie and on the next page load have PHP use the value of that cookie to create the session varibale

Upvotes: 5

Related Questions