Here is javascript code:
var jsonData = JSON.stringify(testObj);
$.ajax({
url: '../php/functions/spesific_field_set.php',
type: 'post',
data: {fieldObjArray: jsonData, tableName: fieldTableName}
}).always(SpesificPropertiesSet);
and here is the php:
$updates = mysql_real_escape_string($_POST['fieldObjArray']);
$updates = json_decode($updates, true);
$tableName = mysql_real_escape_string($_POST['tableName']);
echo $updates;
What testObj is an array of object, how should I pass it to the php? and how should I access the data within this array of objects on php side?
thanks!!
asked Sep 29, 2012 at 23:50
vlio20
9,33518 gold badges104 silver badges188 bronze badges
1 Answer 1
This is the PHP file. This should show you how you can access $updates that was sent through AJAX.
$updates = $_POST['fieldObjArray'];
$updates = json_decode($updates, true);
$tableName = $_POST['tableName'];
echo $updates; // this is an array so this would output 'Array'
foreach ($updates as $key => $value) {
echo 'Key: '.$key.' Value: '.$value.'<br />'; // to access this, just use $updates['key']
}
// example
echo $updates['something'];
answered Sep 29, 2012 at 23:55
rationalboss
5,3973 gold badges32 silver badges50 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
rationalboss
www.php.net/json_decode -->
**assoc**: When TRUE, returned objects will be converted into associative arrays. This was initially set to true in the OP's codeMichael Berkowski
indeed, sorry I missed that. Still though, it is going to be a 2D array, so the 1D loop will just output
Array,Array,Arraydefault
var_dump($updates)after youjson_decode()it to see what it looks like, and you'll have your answer.mysql_real_escape_string()on the input JSON from $_POST..