php - codeigniter ajax form validation

后端 未结 2 1531
梦毁少年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:26

    Use this way i hope this will work for you

    <script type='text/javascript'>
    var base_url = '<?=base_url()?>';
    
    function ajax_call()
    {
       var ids = $("#all_users").val();
    
       $.ajax({
          type:"POST",
          url: base_url+"expense/home/get_expense",
          data: "userid=" + ids,
          success: function(result){
          $("#your_div_id").html(result);
     }
     });
    
    }
    </script>
    
    0 讨论(0)
  • 2020-12-21 20:31

    HTML:

    <div id="ajaxResults"></div>
    

    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.

    0 讨论(0)
提交回复
热议问题