0

I have a JSON array that is on a page. It is added to as time goes on by some user interaction. When the user is done they need to submit this array to a page where their information is added to a mySQL table via php. My call to the page via AJAX is as follows where answersArray is the array

$.ajax({
    type: "POST",
    dataType: "json",
    data: answersArray,
    url: 'sendToUsersFeedback.php',
});

The array looks like this:

[
    {
        "USERID": "3",
        "INDVID": "0",
        "RATING": "1",
        "CONFIDENCE": "8"
    },
    {
        "USERID": "3",
        "INDVID": "1",
        "RATING": "1",
        "CONFIDENCE": "88"
    }
]

This is where I get confused. I need to decode this incoming json and then loop through it added a new record for each array item (USERID, INDVID, RATING and CONFIDENCE make up one record etc..) I could have as many as 20 in this array. I know USERID is not unique. Already have that set up.

It is the php side I get messed up on. How do I decode an incoming array and go through it. I have tried the following

$sql = json_decode(data,true);
foreach( $data as $row ) {
    $sql[] = '("'.mysql_real_escape_string($row['USERID']).'", '.$row['INDVID'].','.$row['RATING'].','.$row['CONFIDENCE'].')';
}
mysql_query('INSERT INTO users_feedback (USERID, INDVID, RATING. CONFIDENCE) VALUES '.implode(',', $sql));

Am I close? I'm very confused. Thanks in advance.

3
  • 1
    You're sending json directly to PHP, but never declaring a $_POST variable name. Should probably look at using 'data':{'data':ansersArray} then change the PHP to use json_decode($_POST['data'],true). Commented Sep 11, 2012 at 19:27
  • Brad, still doesnt like the $_POST['data']. Tell me it isnt valid. I changed the js code to show $('.submitAll').click(function() { $.ajax({ type: "POST", dataType: "json", data:{'data':answersArray}, url: 'sendToUsersFeedback.php', }); }); Commented Sep 11, 2012 at 20:11
  • "RATING. CONFIDENCE" You have point instead comma Commented Mar 20, 2014 at 4:44

3 Answers 3

1

First, give a name to your data you're posting :

$.ajax({
    type: "POST",
    dataType: "json",
    data: {
      my_data: answersArray
    },
    url: 'sendToUsersFeedback.php',
});

Then, in your PHP code, recover your data :

$data = $_POST['my_data'];
$sql = json_decode($data, true);
...

You should be aware that everybody can edit JSON in the client-side, so insert data into a database this way is extremely dangerous.

Sign up to request clarification or add additional context in comments.

6 Comments

this seems to be the hang up. Keeps telling the that $data is undefined. I know the JSON object is legit. Why isnt it coming over?
$('.submitAll').click(function() { $.ajax({ type: "POST", dataType: "json", data:{'data':answersArray}, url: 'sendToUsersFeedback.php', }); });
where is your answersArray declared?
wrestore.iupui.edu/Jon/tool.php Take a look. Scroll down past the table. When you hit the Next button or the Back you will see the answersArray appear. Hitting Submit will fire the script. Thanks much for your help.
Ham, you're using answersArray which is not declared. Replace data:{'data':answersArray}, by data:{'data':$('#JSONHolder').html()},
|
0

You are doing:

$sql = json_decode(data,true); 

What is data? You probably missed the dollar sign. Are you actually getting your JSON in $data?

You're assigning json_decode() result to $sql; now your $sql variable will contain an associative array with your $data. $data is still a string because you didn't change it's value since retrieving it.

Then you're looping through the string (not happening) and running:

mysql_query('INSERT INTO users_feedback (USERID, INDVID, RATING. CONFIDENCE) VALUES '.implode(',', $sql));

$sql above will be your JSON, as a PHP array, something like:

array(array('USERID' => 1 ...), array('USERID' => 2 ...)); 

You should have:

$data = json_decode($data, true);
$sql = array();
foreach ($data as ...

Also, make sure you actually retrive $data:

$data = $_POST['data']

Comments

0

Simple, you have the foreach using the $data array that it's from the json code.

You must set the decoded array as another variable and loop with it.

$j_decoded = json_decode(data,true);

foreach(j_decoded as $row ) {
$sql[] = '("'.mysql_real_escape_string($row['USERID']).'", '.$row['INDVID'].','.$row['RATING'].','.$row['CONFIDENCE'].')';
}
mysql_query('INSERT INTO users_feedback (USERID, INDVID, RATING. CONFIDENCE) VALUES '.implode(',', $sql));

Hope this helps you.

Comments

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.