Codeigniter验证无法使用jQuery / Ajax

时间:2019-02-11 07:20:02

标签: jquery ajax codeigniter

我正在尝试通过jquery ajax使用CodeIgniter验证来验证我的表单,但目前无法正常工作。我不知道我在哪里做错了。那么,如何将CodeIgniter验证与jquery ajax结合使用?请帮助我。

谢谢

<script>
    $(document).ready(function(){
        $("#submit").click(function(){
            var pan = $("#pan_company").val();
            var unino = $("#gst_number").val();
            var res = unino.substr(2, 10);
            if(pan!=res)
            {
                alert("Wrong GSTIN Number...!");
            }
            else
            {
                pan_company = $("#pan_company").val();
                gst_number = $("#gst_number").val();

                $.ajax({
                    type:"POST",
                    data:{"pan_company":pan_company,"gst_number":gst_number},
                    url:"<?php echo base_url(); ?>company",
                    data:$("#FormId").serialize(),,
                    success:function(data){
                        $("#update_detail").html(data);
                    }
                });
            }
        });
    });
</script>

<form id="FormID">
    <div id="update_detail"></div>
    <div class="row clearfix">
        <input type="text" class="form-control" name="pan_company" id="pan_company" placeholder="PAN of Company/Individual *" maxlength="10" >
        <div class="error">
            <?php echo form_error('pan_company'); ?>
        </div>

        <input type="text" class="form-control" name="gst_number" id="gst_number" name="gst_number" placeholder="GST Number" >
        <div class="error">
            <?php echo form_error('gst_number'); ?>
        </div>
    </div>
    <button type="button" id="submit" class="btn btn-primary">Save</button>
</form>

控制器:

public function company()
{
    $this->form_validation->set_rules('pan_company', 'Company PAN', 'required');
    $this->form_validation->set_rules('gst_number', 'Company GST Number', 'required');
    if($this->form_validation->run()==TRUE)
    {
        $pan_company = $this->input->post('pan_company');
        $gst_number = $this->input->post('gst_number');
        echo $pan_company;
        echo $gst_number;
    }
}

1 个答案:

答案 0 :(得分:0)

为了使用验证也要获取错误。在控制器中添加else部分

public function company()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('pan_company', 'Company PAN', 'required');
    $this->form_validation->set_rules('gst_number', 'Company GST Number', 'required');
    if($this->form_validation->run()==TRUE)
    {
        $pan_company = $this->input->post('pan_company');
        $gst_number = $this->input->post('gst_number');
        echo $pan_company;
        echo $gst_number;
    }
    else {
        $error = array();
        if (form_error('pan_company')) {
            $error['pan_company'] = form_error('pan_company');
        }
        if(form_error('gst_number')) {
            $error['gst_number'] = form_error('gst_number');
        }
        echo json_encode($error);
    }
}

并在视图中进行一些更改

<script>
    $(document).ready(function(){
        $("#submit").click(function(){
            var pan = $("#pan_company").val();
            var unino = $("#gst_number").val();
            var res = unino.substr(2, 10);
            if(pan!=res)
            {
                alert("Wrong GSTIN Number...!");
            }
            else
            {
                pan_company = $("#pan_company").val();
                gst_number = $("#gst_number").val();

                $.ajax({
                    type:"POST",
                    data:{"pan_company":pan_company,"gst_number":gst_number},
                    url:"<?php echo base_url(); ?>company",
                    data:$("#FormId").serialize(),
                    success:function(data){
                        var obj = jQuery.parseJSON (data);
                        if(obj.gst_number != "undefined" || obj.pan_company != "undefined"){
                          $("#error-gst_number").html(obj.gst_number);
                          $("#error-pan_company").html(obj.pan_company);
                        } else {
                          $("#update_detail").html(data);
                        }
                     }
                });
            }
        });
    });
</script>

<form id="FormID">
    <div id="update_detail"></div>
    <div class="row clearfix">
        <input type="text" class="form-control" name="pan_company" id="pan_company" placeholder="PAN of Company/Individual *" maxlength="10" >
        <div class="error" id="error-pan_company">
            <?php // echo form_error('pan_company'); ?>
        </div>

        <input type="text" class="form-control" name="gst_number" id="gst_number" name="gst_number" placeholder="GST Number" >
        <div class="error" id="error-gst_number">
            <?php // echo form_error('gst_number'); ?>
        </div>
    </div>
    <button type="button" id="submit" class="btn btn-primary">Save</button>
</form>