简体   繁体   中英

java script array of objects to php via ajax (jquery) and json

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

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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM