php - codeigniter ajax form validation

后端 未结 2 1535
梦毁少年i
梦毁少年i 2020-12-21 19:44

Hi I’m quite new to jquery -ajax and I’d like some help please to join it with CI.

I have followed this tutorial on Submitting a Form with AJAX and I’d like to add t

2条回答
  •  粉色の甜心
    2020-12-21 20:31

    HTML:

    Javascript ajax:

     success: function(response) {
        $('#ajaxResults').text(response);
       }
    

    this script you've wrote is only if the validation succeeds, right?

    Wrong. The code in "success" gets executed any time you get a response back from the server (assuming the HTTP header is 200). Does your javascript knows if the server has any error for you? No.

    You need your JavaScript to recognize if the validation failed or succeeded. You have many ways to do that. One of these could be sending the message to display followed by a 0 or 1.

    So your PHP will looks like:

    return "0 " . $errorMessage;
    

    and

    return "1 " . $successMessage;
    

    and your javascript should then recognize, with if statement and substring, if the message starts with 0 or with 1.

提交回复
热议问题