1

I'm pretty new to PHP, and I'm currently using AJAX to validate a form upon a button click.

Currently, I'm using an if statement to see whether to continue on to the next page of a multi-step form or not.

 $.ajax({
      url: 'index.php',
      type: 'POST',
      data: {
        myid: 1,
        action: action,
        username: user,
        password: pass
      },
      success: function(data){
          if (data == "continue") {
            wizard.next(wizard);
            wizard.showButtons();
          } else {
            helpframe.text(data);
            helpframe.show();
          }
      }
  });

I'm wondering when to simply use an if statement like above in the return AJAX function vs. throwing an error in PHP, since it's my understanding that you can somehow use code to purposely cause PHP to throw an error and then execute different code upon a "failure".

Buck
  • 152
  • 1
  • 8
  • 2
    Yep. PHP would send a different header response if there's a fatal error or something else developer needs to know. – mehulmpt Jun 18 '15 at 16:37
  • 2
    But doing that also means you can't easily distinguish between a run-time error and a thrown exception - that is if you throw say a 500 HTTP response for both or something. I'd use a 200 response and have your PHP code send back a structured JSON response that is parsed client side... see this: http://stackoverflow.com/questions/12806386/standard-json-api-response-format – ficuscr Jun 18 '15 at 16:39

0 Answers0