user2028856
user2028856

Reputation: 3183

Setting PHP Session variables with jQuery Ajax

I have a pretty simple issue which I just can't seem to resolve. I have the following ajax request which sets a PHP Session variable

  $.post("http://mytestdomain.com/test.php", {"data": 'success'});

And this code in the PHP file to generate and echo the Session variables

  session_start();
  $_SESSION['test_text']= $_POST['data'];
  echo "Pageviews=". $_SESSION['test_text'];    

However this keeps returning the following error message

  Notice: Undefined index: data in /var/www/test.php on line 2

If I post a demo URL into my browser like this

 http://mytestdomain.com/test.php?data=11111

Then the results are echoed correctly.

So my question is, how do I pass via jQuery Ajax data to a PHP session variable and have it saved?

Thanks

Upvotes: 0

Views: 432

Answers (1)

In your test.php file, try the following:

 session_start();
 $_SESSION['test_text']= $_REQUEST['data'];
 echo "Pageviews=". $_SESSION['test_text'];    

Upvotes: 1

Related Questions