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!!

share|improve this question

var_dump($updates) after you json_decode() it to see what it looks like, and you'll have your answer. – Michael Berkowski yesterday
1  
By the way, unless you are inserting the whole JSON string directly into your database (which may be indicative of a poor design), don't call mysql_real_escape_string() on the input JSON from $_POST.. – Michael Berkowski yesterday
Learn about arrays: php.net/manual/en/language.types.array.php – Felix Kling yesterday
feedback

1 Answer

up vote 0 down vote accepted

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'];
share|improve this answer
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 code – rationalboss yesterday
indeed, sorry I missed that. Still though, it is going to be a 2D array, so the 1D loop will just output Array,Array,Array – Michael Berkowski yesterday
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.