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;
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;
Share
Improve this question
edited Jun 12, 2014 at 11:37
hsz
152k62 gold badges267 silver badges320 bronze badges
asked Jun 12, 2014 at 9:56
URL87URL87
11k36 gold badges111 silver badges177 bronze badges
3 Answers
Reset to default 5If 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'];
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.
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);