I want to send some data in json format to php and do some operation in php. My problem is i can't send json data via ajax to my php file.Please help me how can i do that. I have tried this way..
<script>
$(function (){
$("#add-cart").click(function(){
var bid=$('#bid').val();
var myqty=new Array()
var myprice=new Array()
qty1=$('#qty10').val();
qty2=$('#qty11').val();
qty3=$('#qty12').val();
price1=$('#price1').val();
price2=$('#price2').val();
price3=$('#price3').val();
var postData =
{
"bid":bid,
"location1":"1","quantity1":qty1,"price1":price1,
"location2":"2","quantity2":qty2,"price2":price2,
"location3":"3","quantity3":qty3,"price3":price3
}
var dataString = JSON.stringify(postData);
$.ajax({
type: "POST",
dataType: "json",
url: "add_cart.php",
data: {myData:dataString},
contentType: "application/json; charset=utf-8",
success: function(data){
alert('Items added');
},
error: function(e){
console.log(e.message);
}
});
});
});
</script>
And in PHP i use:
if(isset($_POST['myData'])){
$obj = json_decode($_POST['myData']);
//some php operation
}
When in add print_r($_POST) in php file, it shows array(0) {} in firebug.
I believe you could try something like this:
var postData =
{
"bid":bid,
"location1":"1","quantity1":qty1,"price1":price1,
"location2":"2","quantity2":qty2,"price2":price2,
"location3":"3","quantity3":qty3,"price3":price3
}
$.ajax({
type: "POST",
dataType: "json",
url: "add_cart.php",
data: postData,
success: function(data){
alert('Items added');
},
error: function(e){
console.log(e.message);
}
});
the json encode should happen automatically, and a dump of your post should give you something like:
array(
"bid"=>bid,
"location1"=>"1",
"quantity1"=>qty1,
"price1"=>price1,
"location2"=>"2",
"quantity2"=>qty2,
"price2"=>price2,
"location3"=>"3",
"quantity3"=>qty3,
"price3"=>price3
)
That's because $_POST
is pre-populated with form data.
To get JSON data (or any raw input), use php://input
.
$json = json_decode(file_get_contents("php://input"));
If you want to get the values via the $_POST
variable then you should not specify the contentType as "application/json"
but rather use the default "application/x-www-form-urlencoded; charset=UTF-8"
:
JavaScript:
var person = { name: "John" };
$.ajax({
//contentType: "application/json", // php://input
contentType: "application/x-www-form-urlencoded; charset=UTF-8", // $_POST
dataType : "json",
method: "POST",
url: "http://localhost/test/test.php",
data: {data: person}
})
.done(function(data) {
console.log("test: ", data);
$("#result").text(data.name);
})
.fail(function(data) {
console.log("error: ", data);
});
PHP:
<?php
// $_POST
$jsonString = $_POST['data'];
$newJsonString = json_encode($jsonString);
header('Content-Type: application/json');
echo $newJsonString;
Else if you want to send a JSON from JavaScript to PHP:
JavaScript:
var person = { name: "John" };
$.ajax({
contentType: "application/json", // php://input
//contentType: "application/x-www-form-urlencoded; charset=UTF-8", // $_POST
dataType : "json",
method: "POST",
url: "http://localhost/test/test.php",
data: person
})
.done(function(data) {
console.log("test: ", data);
$("#result").text(data.name);
})
.fail(function(data) {
console.log("error: ", data);
});
PHP:
<?php
$jsonString = file_get_contents("php://input");
$phpObject = json_decode($jsonString);
$newJsonString = json_encode($phpObject);
header('Content-Type: application/json');
echo $newJsonString;
To send javascript obj to php using json and ajax:
js:
var dataPost = {
"var": "foo"
};
var dataString = JSON.stringify(dataPost);
$.ajax({
url: 'server.php',
data: {myData: dataString},
type: 'POST',
success: function(response) {
alert(response);
}
});
to use that object in php:
$obj = json_decode($_POST["myData"]);
echo $obj->var;
just remove:
...
//dataType: "json",
url: "index.php",
data: {myData:postData},
//contentType: "application/json; charset=utf-8",
...
I know it's been a while, but just in case someone still needs it:
The JSON object I need to pass:
0:{CommunityId: 509, ListingKey: "20281", Type: 10, Name: "", District: "", Description: "",…}
1:{CommunityId: 510, ListingKey: "20281", Type: 10, Name: "", District: "", Description: "",…}
The Ajax code:
data: JSON.stringify(The-data-shows-above),
type: 'POST',
datatype: 'JSON',
contentType: "application/json; charset=utf-8"
And the PHP side:
json_decode(file_get_contents("php://input"));
It works for me, hope it can help!
You are tryng to send js array with js object format.
Instead of use
var a = new array();
a['something']=...
try:
var a = new Object();
a.something = ...
Source: Stackoverflow.com