the following code is not working as expected, the echo statement never return any response to the client side and it is hang.
$query = "INSERT INTO Tasks (ProjectID,Title,Start,End,PercentComplete,ParentID,OrderID,Summary,Expanded,LastUpdate)
VALUES($project_Id, '$title','$start','$end','$percentComplete',$parentID,'$orderID','$summary','$expanded',NOW())";
$result = mysqli_query($con, $query);
if ($result) {
$last_id = mysqli_insert_id($con);
echo json_encode(array(ID => $last_id, Title => $title, Start => $start, End => $end, percentComplete => $percentComplete));
}
However, if I added one more line of echo as follows, both the echo statement is able to be received on the client side.
$query = "INSERT INTO Tasks (ProjectID,Title,Start,End,PercentComplete,ParentID,OrderID,Summary,Expanded,LastUpdate)
VALUES($project_Id, '$title','$start','$end','$percentComplete',$parentID,'$orderID','$summary','$expanded',NOW())";
$result = mysqli_query($con, $query);
if ($result) {
$last_id = mysqli_insert_id($con);
echo json_encode(array(ID => $last_id, Title => $title, Start => $start, End => $end, percentComplete => $percentComplete));
echo 1;
}
I cannot figure out what is wrong here, please help to advice on this. Thanks
You are defining your array keys as constants. They need to be quoted. take a look at the following array definiton.
$array = array(
'id' => $id,
'title' => $title,
);
Notice that I have used '
to quote my keys?
You can identify your issue by enabling error reporting:
error_reporting(E_ALL);
ini_set('display_errors', 'On');
It will then show you an error:
Notice: Undefined constant '...'
To explain a little better what's going wrong in your syntax you will first need to understand what constants
are. Constants are similiar to variables. The only difference is that they have to be defined at the top of your script and that the value can not be changed (or better said is constant).
To define a constant you would use:
define('constantname', 'constantvalue');
However, for your code to work you don't need constants but strings. What you want is to define your array keys as a string.
'key' => 'value'
I hope this will fix your problem.
Your echo should look like
$json = json_encode(array(
'ID' => $last_id,
'Title' => $title,
'Start' => $start,
'End' => $end,
'percentComplete' => $percentComplete
));
echo $json;
Try setting the output headers in your controller like this (before the output is sent back i.e. before json_encode):
$this->output->set_header('Content-Type: application/json; charset=utf-8');
And as mentioned in earlier answer, use quotes around your array keys.
it turns out that there is a for loop in the javascript which initiate the request. The for loop send lot of $_POST and the response is actually returned to client side after a long time (~5 minutes).