1

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.

5
  • Does the success callback get triggered? Commented Jan 16, 2014 at 14:02
  • cant tell because the bootbox.dialog will not showup..when i tried to change the json_encode in controller into echo $id the dialog box will show..but i cant continue coz in my callback: will get a value from json_encode. Commented Jan 16, 2014 at 14:07
  • 1
    You can not tell? A simple console.log() or break point tells you that. Add a error handler to the Ajax call. Commented Jan 16, 2014 at 14:08
  • sorry..i edited my question a little..coz i find it strange. the custom dialog will only show after i refresh the page.. Commented Jan 16, 2014 at 14:13
  • So it runs once and you expect it to run again? If you want the function to run again, you need to call a new timeout, you are not running an interval. Commented Jan 16, 2014 at 14:37

1 Answer 1

1

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.

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

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.