Do form validation with jquery ajax in codeigniter

后端 未结 6 1023
猫巷女王i
猫巷女王i 2020-12-03 22:34

How can i do form validation in codeigniter if i don\'t want to refresh the page? Basically i do this:

    $config = array(
            array(
                       


        
6条回答
  •  盖世英雄少女心
    2020-12-03 23:07

    If you are aware about passing data with ajax, then the work flow is as follows.

    1) Send form data through ajax to your controller.

    2) Do form validation as of now.

    3) If success then "echo" value 1

    4) If failure, echo value 0

    So that using the value in echo, you can determine whether the validation fails or not. I can give you an example if you need

    Sample ajax code

    $('#form').on('submit', function(e){
        e.preventDefault();
        var data = $(this).serialize();
        $.ajax({
            url: 'your url',
            type: 'POST',
            data: data,
            success: function(data){
                if(data == 1){
                    $('#form')[0].reset();
                    alret('success');
                }
                else if(data == 0){
                    alret('failed');
                }
            },
            error: function(){
                alert('error');
            }
        });
    });
    

提交回复
热议问题