Sending Javascript Object to PHP via Ajax
If your array has more then 1 dimension or is an associative array you should use JSON.
Json turns a complete array structure into a string.
This string can easily send to your php application and turned back into a php array.
More information on json: http://www.json.org/js.html
var my_array = { ... };
var json = JSON.stringify( my_array );
In php you can decode the string with json_decode:http://www.php.net/manual/en/function.json-decode.php
var_dump(json_decode($json));
Pass object to PHP through AJAX
from the second ajax you can access the data based on the property names like: $_GET['data']['field01']
$_GET['data']
is the js object converted in php in a associative array
Send javascript object with AJAX
If you want to be absolutely sure, that you get the same object back from PHP (no conversion what so ever), you can serialize it as a string (JSON.stringify(object)
) and afterwards parse it again.
Here is one solution:
JavaScript (send data):
var data = [
{ "abc": "def" },
{ "ghi": "jkl" }
];
$.ajax({
"url": "http://localhost/savedata.php",
"method": "POST",
"data": {
"list": JSON.stringify(data)
}
});
PHP (savedata.php
):<?php
session_start();
$_SESSION['data'] = $_POST['list'];
?>
PHP (getdata.php
)<?php
session_start();
header("Content-Type: application/json");
echo $_SESSION['data'];
?>
JavaScript (receive data):var data = [];
$.ajax({
"url": "http://localhost/getdata.php",
"method": "GET",
"success": function(response) {
data = response;
}
});
How to send javascript object to php via ajax?
According to this page (https://datatables.net/forums/discussion/30848/trying-to-get-get-the-row-ids-using-rows-ids), you are getting a dataTables object instance. To get an array, you'd do this:
table.rows( { selected: true } ).ids().toArray();
It might help to simplify what you're working with, the dataTables object might be what's going cyclic on you. passing JavaScript object to PHP not working
There are 2 problems.
- You are not sending any data with the request
- That's not the way you'll get the value from a request in PHP
$.ajax({
type: 'POST',
url: 'ajax.php',
data : { json: json }, // <---------------------
...
* this works just because jQuery implementation will automatically convert any non-string data argument into a form-urlencoded query string. See the docs.Then, in your PHP, you should do:
$jsonStr = $_POST['json'];
$json = json_decode($jsonStr);
Edit:
Another possible way:
$.ajax({
type: 'POST',
url: 'ajax.php',
data : json , // <---------------------
...
This way, your data will not be a valid form-urlencoded
input, so PHP will not parse it into $_POST
, but you still can get the contents of your input doing this:$jsonStr = file_get_contents("php://input");
$json = json_decode($jsonStr);
cannot pass a javascript object to php via ajax
You are trying to use your array as a hash, so the values are not being set..
Instead of setting
var advancedFormVars = new Array();
Try setting
var advancedFormVars = {};
Example
JS:
var advancedFormVars = {};
advancedFormVars['checkbox1'] = 'valueA';
advancedFormVars['checkbox2'] = 'valueB';
var json = JSON.stringify(advancedFormVars);
console.log(json); //{"checkbox1":"valueA","checkbox2":"valueB"}
PHP<?php
$json = '{"checkbox1":"valueA","checkbox2":"valueB"}';
$obj = json_decode($json);
var_dump($obj);
/*
object(stdClass)#1 (2) {
["checkbox1"]=>
string(6) "valueA"
["checkbox2"]=>
string(6) "valueB"
}
*/
?>
javascript object to php using jquery ajax
Can send the JSON and use json_decode()
to turn it into a php array.
$.post('server/path', { jsonData: JSON.stringify(arr)}, function(response){
/* do something with response if needed*/
});
in php:$arr=json_decode( $_POST['jsonData'] );
/* return first ID as test*/
echo $arr[0]['id'];
/* or dump whole array as response to ajax:*/
print_r($arr);
JQuery object to PHP via ajax to send a mail()
You have to give the properties of your data object names, otherwise you will get errors:
$datastring = {
surname : $surname,
name : $name,
email : $email,
comment : $comment,
to : 'asd@efg.de',
subject : 'Contact Form'
};
On PHP side you get the values by this names again:$surname = $_POST["surname"];
$name = $_POST["name"];
$email = $_POST["email"];
$comment = $_POST["comment"];
$to = $_POST["to"];
$subject = $_POST["subject"];
Javascript Object via AJAX to PHP Array
You can't concatenate an object and a string
Just send the object as data
and jQuery will serialize it internally
$.ajax({
type: "POST",
data: bodycontent ,
cache: false,// pointless since POST doesn't get cached by browser
url: 'url.php',
success: function(html) {
alert(html);
}
})
Then in php look for the individual keys within your object like:$_POST['search']
$_POST['query']// will be array since that's what you are sending
etc
Passing javascript object to php file using ajax post method
You have to encode it to json if you want to send it as json.
xmlhttp.send("data="+encodeURIComponent(JSON.stringify(json)));
currently what you have will send something like data=[Object object]
.The variable json
is a JavaScript object which is not json. JSON is a data-interchange format which is basicly a subset of javascript. see http://json.org
var object = {"name" : "Darth Vader"};// a JavaScript object
var json = '{"name" : "Darth Vader"}';// json holds a json string
Related Topics
How to Skip Elements in Foreach Loop
Setting Up PHPmailer with Office365 Smtp
How to Implement Ws-Security 1.1 in PHP5
Laravel 4 - Including a "Partial" View Within a View (Without Using Blade Template)
Uploading Image from Android to PHP Server
How to Add Custom Fields to Woocommerce Registration Form
Find Array Key in Objects Array Given an Attribute Value
Cannot Unpack Array with String Keys
PHP MySQL Query Where X = $Variable
Php, How to Get Current Date in Certain Format
PHP Date Function Seven Days Previous
Encoding Byte Data into Digits
Use of Undefined Constant Mcrypt_Rijndael_128 - Assumed 'Mcrypt_Rijndael_128'
PHP Local Server Invalid Request (Unexpected Eof)
How to Access Outer Local Variable in PHP