Reputation: 301
AJAX function
var stopTime = 0;
var checkRequApprove = function ()
{
$.ajax({
url: 'http://127.0.0.1/ProgVsProg/main/checkApproveReq',
success:function(output){
jsn=JSON.parse(output);
if(output==false){
stopTime = setTimeout(checkRequApprove, 3000);
}
else {
bootbox.dialog({
message: "Battle",
title: "Request Accepted",
buttons:
{
success: {
label: "Approve",
className: "btn-success",
callback: function() {
$.ajax({
"type" : "POST",
"url" : "finalBattleApprove",
"data" : {'username' : jsn.user_name},
success: function(data){
$('#example').html(data);
$('#loadingmessage').show();
}
});
}
},
}
}
});
}
stopTime = setTimeout(checkRequApprove,3000);
Controller
public function checkApproveReq(){
$id = $this->session->userdata('userID');
$requApprove = '1';
$check = $this->lawmodel->checkRequApprove($id,$requApprove);
foreach($check as $row){
if($row->requApprove == '1')
{
$reqID = $this->lawmodel->getID($row->requestedID);
foreach($reqID as $row){
echo json_encode(
array(
'user_name' =>$row->username,
)
);
}
}
else
echo false;
}
}
I have this code wherein there is a realtime check to the database and if the condition has meet..custom dialog will pop up.
Im having this problem on my checkRequApprove
function..The bootbox.dialog
will not showup if the condition is meet in the controller...
i believe that my problem is because in the controller which echo json_encode
. I cant find any solution .im still a newbie in ajax..
EDITED the custom dialog will only show after i refresh the page.
Upvotes: 1
Views: 136
Reputation: 51191
output
is a string - the comparison output == false
will always yield false, because a non-empty string is always true (not considering the edgecase "0" in which case "0" == false yields true).
Instead of
if(output==false){
...
}
it should be:
if (!jsn){ // or jsn == false if you like that better
...
}
Also you should consider not returning a simple value in your controller, but always a proper json-object like:
else{ // consider always using brackets, for more robustness
echo
array(
'error' => true
);
}
Now, in your js you just check for
if (jsn.error){
...
}
In any case, you should include an error callback for your json to handle possible errors with the json request.
Upvotes: 1