3

I am very puzzled. I found a number of post with very similar question either go unanswered or all the answer says it should work. or says it worked.

I passed an key value pair object from jquery to php. and alert it back out again successfully but if I go to the php page. it says the data is null or undefined index.

below is my jquery

$('#test').click(function(){
    var obj= {
        'key': 'value',
    };
    $.ajax({
        url: "../folder/file.php",
        type: "POST", 
        data: {
            'obj' : obj
        },
        dataType: "JSON",
        success:function(data){
            alert(data);
        }

    });
}); 

below is my php

$data = $_POST['obj']; // this is line 1
echo json_encode($data); // this is line 2

With the above code, when I click test button, I will get alert value. but if I go to the php page after I clikced the test button. the page says Notice: Undefined index: obj on line 1, and null on line 2.

if I change the php side to below. I will get there is nothing when I go to the php page but my original page is alerting value

if(!empty($_POST['obj'])){
    $data = $_POST['obj'];
    echo json_encode($data); 
}else{
    echo 'there is nothing';
}  

Why?

I am getting alerted the value I put in. So it must mean the data went through and back. but the php page says otherwise

3
  • 1
    Variables don't persist between different executions of the script, except for session variables. Commented Aug 25, 2016 at 20:32
  • that that means the data was there when I clicked my test button, but when I loaded the php page it went away? Commented Aug 25, 2016 at 20:34
  • You code seems to be good. The same issue occurred with mine, but then I tested in another server and all is OK :) I think it was a browser cache problem. Commented Nov 1, 2020 at 19:00

2 Answers 2

3

When you send the AJAX request, that sends the POST parameter in the data: option, and the script can access it in $_POST.

When you go to the URL directly from the browser, that doesn't send any POST parameters, so $_POST is empty.

Each time you execute a PHP script, it starts a fresh process. Variables set in a previous script are not retained. The exception is $_SESSION, if you have used session_start() in the scripts.

Sign up to request clarification or add additional context in comments.

6 Comments

ok, my question was a sub issue of a larger issue. I originally was trying to pass object with many object in it. is that possible via jquery ajax. I see the jquery document says it can only do plain object.
Yes, you can. It will URL-encode the object, and PHP will decode it automatically.
How can I open my php page during the same ajax request. so I can actually var_dump my new object and test if things are working.
You can use the Network tab of Developer Tools. Perform the AJAX request, then select its URL, then go to the Response tab to see everything echoed by the script.
can ajax open a new window to the same page it is posting data to. I successfully passed data to my php to generate a pdf using mpdf. now I want to prompt download pdf. if I am on the php page it would prompt the download. I try to success function return the pdf as string. it just froze the Developer Tools for some reason.
|
2

You are not understanding how HTTP works, HTTP is stateless if you want a value to be stored, use PHP session_start() and store the value from post to $_SESSION

Comments

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.