0

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

3
  • So can you do post much more complete code? Commented Aug 30, 2014 at 8:06
  • This is the complete code, it's for testing purposes Commented Aug 30, 2014 at 8:06
  • Try doing var_dump($_POST) and post the result Commented Aug 30, 2014 at 8:34

1 Answer 1

1

In your test.php file, try the following:

 session_start();
 $_SESSION['test_text']= $_REQUEST['data'];
 echo "Pageviews=". $_SESSION['test_text'];    
Sign up to request clarification or add additional context in comments.

1 Comment

Check test here, using your code but with a $_REQUEST method. checkourprogress.co.uk/test/test.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.