0

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

4
  • var_dump($updates) after you json_decode() it to see what it looks like, and you'll have your answer. Commented Sep 29, 2012 at 23:52
  • 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.. Commented Sep 29, 2012 at 23:53
  • possible duplicate of How to pass data from Javascript to PHP and vice versa? Commented Sep 29, 2012 at 23:54
  • Learn about arrays: php.net/manual/en/language.types.array.php Commented Sep 30, 2012 at 0:07

1 Answer 1

2

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'];
2
  • 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 Commented Sep 30, 2012 at 0:00
  • 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 Commented Sep 30, 2012 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.