2

Suppose there is the javascript with ajax request -

index.js-

var dataFeedback = $("#feedback_popup_message_body").val();
var jsonObj = JSON.stringify({dataFeedback: dataFeedback});
$.ajax({
    url: "index.php",
    type: 'POST',
    data: jsonObj,
    dataType: 'json',
    success: function (data) {
        console.log(data);
    }
});

and in the server side , php page -

index.php-

<?php
    $myPostData = json_decode($_POST["dataFeedback"]);
    $feedback = $myPostData["dataFeedback"];
    echo $feedback;
?>

I try to send with the request a json object and once it parsed in the server side, get it back to the client page and logs its data .

In the above concept it doesn't logs the data value .

I checked it in Chrome > F12 > Networks > index.php > Response and found this -

<br />
<b>Notice</b>:  Undefined index: dataFeedback in <b>...\index.php</b> on line <b>11</b><br />

How to cause it to logs the data which backs from the server ?

Update: The code that finally cause it works -

index.js-

var dataFeedback = $("#feedback_popup_message_body").val();

$.ajax({
    url: "bin/bll/suggestionSummary.php",
    type: 'POST',
    data: {dataFeedback: dataFeedback},
    success: function (data) {

        console.log(data);
    }
});

index.php-

<?php
    $myPostData = $_POST['dataFeedback'];
    echo $myPostData;

3 Answers 3

5

If you have simply value than why are you using JSON.stringify? Try this...

var dataFeedback =  $("#feedback_popup_message_body").val();
var jsonObj  = JSON.stringify({dataFeedback:dataFeedback});
$.ajax({
    url: "index.php",
    type: 'POST',
    data: { dataFeedback:dataFeedback },
    success: function(data){
        console.log(data);
    }
});

Now you can get value direct with $_POST['dataFeedback'];

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

3 Comments

Tried it , now it doesn't appear any response in - Chrome > F12 > Networks > index.php > Response but still doesn't logs the data which back from the server .
Are you sure your php file not sending empty response. Please check it. May be you find something. Use alert your response.
Ok ... it work with just $myPostData = $_POST['dataFeedback']; in the php with no more parsing . I'll update the works code in my post .
1

You're sending raw JSON as the request data, so you can't access it using $_POST on the PHP side, because that only works with form encoded key/value pairs.

You will need to access the raw post data like so, on the PHP side:

$data = json_decode($HTTP_RAW_POST_DATA);
echo $data->dataFeedback;

Finally, you are not outputting JSON on the PHP side, so remove dataType: 'json',, otherwise jQuery will try to parse the response as JSON and invoke the error handler.

Comments

1

jsonObj is a plain string not an array anymore.

What you probably want:

var dataFeedback =  $("#feedback_popup_message_body").val();
    var jsonObj  = JSON.stringify(dataFeedback);
    $.ajax({
        url: "index.php",
        type: 'POST',
        data : {dataFeedback: jsonObj },
        dataType: 'json',
        success: function(data){
            console.log(data);
        }
    });

Now $_POST['dataFeedback'] will be set.

If you want to use associative arrays after json_decode you need to do (notice the second parameter true):

$myPostData = json_decode($_POST["dataFeedback"], true);

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.