Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
So can you do post much more complete code? –  George Garchagudashvili Aug 30 at 8:06
    
This is the complete code, it's for testing purposes –  user2028856 Aug 30 at 8:06
    
Try doing var_dump($_POST) and post the result –  Khalid Dabjan Aug 30 at 8:34

2 Answers 2

In your test.php file, try the following:

 session_start();
 $_SESSION['test_text']= $_REQUEST['data'];
 echo "Pageviews=". $_SESSION['test_text'];    
share|improve this answer
    
Exactly the same result... –  user2028856 Aug 30 at 8:32
1  
Check test here, using your code but with a $_REQUEST method. checkourprogress.co.uk/test/test.html –  Telford Computer-DoctorLtd Aug 30 at 8:33

do data instead of "data"

$.post("http://mytestdomain.com/test.php", {data: 'success'});
share|improve this answer
    
Still the same issue –  user2028856 Aug 30 at 8:03
1  
Both data and "data" are the same –  hindmost Aug 30 at 8:04
    
yes I've found it now –  George Garchagudashvili Aug 30 at 8:05
    
My situation isn't that the ajax request itself has an issue, the ajax request works fine. It's just that the PHP Session doesn't seem to remember anything –  user2028856 Aug 30 at 8:07
    
no it's not session either, problem resides in POST request –  George Garchagudashvili Aug 30 at 8:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.