简体   繁体   中英

Codeigniter ajax form validation

I achieved the ajax form validation. But even though all input types have values in it. It shows the $this->form_validation->run() false . I am not getting how to resolve this issue.

Controller code


$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('balance', 'Balance', 'trim|required|numeric');


    if ($this->form_validation->run() == FALSE) {
        echo validation_errors();


    }else{

    }

AJAX call


$('#updateUsersInfo').on('click',function(){


 $.ajax({
        url:"<?php echo base_url();?>admin/updateUserData",
        type: "POST",
        dataType: 'json',
        data: {'submit_token': $('#token_id').val()},
        success:function(data){

            $('#bindErrors').text(data);


        }

    }) ;
});

View code


<form class="popupform" method="post">
            <div class="error" id="bindErrors"></div>
                <div  id="bindSuccessMsg"></div>
            <div class="row">
                <div class = "col-md-8 col-md-offset-2">
                    <div class="col-md-8">
                        <div class="form-group">
                            First name <input type="text" class="form-control" placeholder="First Name" id="first_name" name="first_name" value="">
                        </div>
                        <div class="form-group">
                            Last name <input type="text" class="form-control" placeholder="Last Name" name="last_name" id="last_name" value="">
                        </div>
                        <div class="form-group">
                            Email <input type="email" id="email" class="form-control" readonly placeholder="Email address" name="email_address" value="">
                        </div>
                        <div class="form-group">
                            Balance <input type="number" id="balance" class="form-control" placeholder="Balance" name="balance" value="">
                        </div>
                        <div class="form-group">
                            <div class="radio" >
                                <label style="margin-right: 10px;">
                                    <input type="radio"  name="account_type" value="1"  >Personal</label>
                            </div>
                            <div class="radio">
                                <label><input type="radio"  name="account_type"  value="3">Business</label>
                            </div>
                        </div>
                        <div class="form-group" style="clear:left; padding-top: 10px;">
                            <input type="hidden" name="user_id" id="user_id" value=""/>

                            <input type="submit" id="updateUsersInfo" class="btn tf-btn btn-default" value="Submit"/>
                        </div>
                    </div>
                </div>
            </div>
            </form>

I have used CSRF token, But now in code don't consider that. apart from that can some one let me know why error shows up even though fields are with valid data.

I am using codeigniter 3.0.3

In this line echo validation_errors(); what you have to do is echo json_encode(validation_errors()) because in ajax call you are using dataType: 'json', and success:function(data){ console.log(data); just try these things it will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM