Get PHP parameters using jQuery ajax post

I send data via jQuery.ajax method to my PHP file. Both files are in the same domain. The file that creates the message is as follows.

$('#pdf').click(function() {                    
    var proj_name = $('#proj_name').text();
    var date = $('#date').text();
    var req_comp_date = $('#req_comp_date').text();
    var status = $('#status').text();
    var secondUserID = $('#secondUserID').text();

    var postData = {
        "proj_name" : proj_name,
        "date" : date,
        "req_comp_date" : req_comp_date,
        "status" : status,
        "secondUserID" : secondUserID,
    };

    console.log(postData);

    $.ajax({
        type: "POST",
        url: "test.php",
        data: postData, 
        success: function(){
            alert(proj_name + ' ' + status);
            window.open("test.php"); 
        }
    });
});

And the PHP file receiving the message data is ...

//request parameters
$proj_name = $_POST['proj_name'];
$date = $_POST['date'];
$req_comp_date = $_POST['req_comp_date'];
$status = $_POST['status'];
$secondUserId = $_POST['secondUserId'];

echo 'postData: ' . var_dump($_POST);

if ($_POST)){
    echo $proj_name;
    echo $date;
    echo $req_comp_date;
    echo $status;
    echo $secondUserId;
} else {
    echo 'problem';
}

In my firebug console, I see the parameters sent with .ajax, but I can not get the message through PHP. Can someone help me please? Thank.

+5
source share
1 answer

Add an error callback to your $.ajaxdebug call if the request does not work.

$.ajax({
    type: "POST",
    url: "test.php",
    data: postData, 
    success: function(){
        alert(proj_name + ' ' + status);
        window.open("test.php"); 
    },
    // Alert status code and error if fail
    error: function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(thrownError);
    }
});

<h / "> Update

Change this:

if ($_POST)){
    echo $proj_name;
    echo $date;
    echo $req_comp_date;
    echo $status;
    echo $secondUserId;
} else {
    echo 'problem';
}

For this:

if ($_POST)){
    // Make a array with the values
    $vals = array(
        'proj_name'     => $proj_name,
        'date'          => $date,
        'req_comp_date' => $req_comp_date,
        'status'        => $status,      
        'secondUserId'  => $secondUserid
    );

    // Now we want to JSON encode these values to send them to $.ajax success.
    echo json_encode($vals);

    exit; // to make sure you arn't getting nothing else

} else {
    // so you can access the error message in jQuery
    echo json_encode(array('errror' => TRUE, 'message' => 'a problem occured'));
    exit;
}

Now in your jQuery callback .success:

success: function(data){ // Our returned data from PHP is stored in "data" as a JSON Object
    alert(data.req_comp_date); // access your returned vars like this.
    // data.date; // is your posted date.. etc
    alert(data.proj_name + ' ' + data.status);
    window.open("test.php"); 

    // You can also get your error message like so..
    if(data.error) // if its true, we have a error, so display it.
         alert('ERROR: ' + data.message); 

},

(jquery ), , , .

$.ajax({ ...
    type: "POST",
    url: "test.php",
    data: postData, 
    dataType: "json" // <-- Add this to tell jquery, we are being returned a JSON object.
.... });
+7

All Articles