I’m cutting my teeth on extjs and running into an issue. I have a function that is updating from a grid table, and I’m trying to send the “success” back to the browser in a JSON encoded statement. If I only update a single value, it works fine. If I update multiple values (from an array), however, the response is sent for each, thus causing an error. How can I give one response for the total array?
Code below:
$conn = mysql_connect ('localhost', 'root', '') or die (mysql_error ());
mysql_select_db ('cptestdata', $conn ) or die (mysql_error ());
$json_string = file_get_contents("php://input");
$obj = json_decode($json_string, true);
// check for single update value. if so, create array wrapper
if(!isset($obj [0])) {
$temp = array();
$temp[] = $obj;
$obj = $temp;
}
foreach ($obj as $value) {
$option_id = $value['option_id'];
$option_val = $value['option_value'];
$option_q = "
UPDATE wp_options
SET option_value='".$option_val."'
WHERE option_id='".$option_id."'
";
mysql_query($option_q);
// json output to notify the insert is success or not
if ($option_q) {
echo '{"success":"true"}';
}
else {
echo '{"success":"false"}';
}
} // endforeach
Remove the
echofrom the loop, like this: