在Codeigniter中使用jQuery Ajax进行表单验证 [英] Do form validation with jquery ajax in codeigniter

查看:98
本文介绍了在Codeigniter中使用jQuery Ajax进行表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不想刷新页面,该如何在Codeigniter中进行表单验证?
基本上我是这样的:

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

    $config = array(
            array(
                    'field' => 'c_name',
                    'label' => 'Name',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'c_job',
                    'label' => 'Job',
                    'rules' => 'trim|required',
                    )
    );
    $this->form_validation->set_rules($config);
    if($this->form_validation->run() == true)
            {
                $this->load->model('model');
                //.....
            }
    else{
            $this->load->view('view');
        }

但是如果我使用ajax发送数据并且页面没有刷新,如何我可以进行表单验证吗?

But if i send data with ajax and the page doesn't refresh, How can i do form validation?

编辑:

感谢Amra Kojon。很好并且可行,但是新的问题是这样:

Thanks @ Amra Kojon. That's good and works but the new problem is this:

if ($this->form_validation->run() == FALSE) {
                echo validation_errors();
                } 
                else {
                        //echo 'hi';


                        $value = $this->input->post('value');

                        $values = array(
                                'c_name' => $value['c_name'],
                                'c_job'=> $value['c_job'],
                                'c_address'=> $value['c_address'],
                                'c_phone'=> $value['c_phone'],
                                'c_mail'=> $value['c_mail'],
                                'c_state'=> $value['c_state'],
                                'c_intrest'=> $value['c_intrest'],
                                'c_added_info'=> $value['c_added_info']
                        );


                        $add = $this->customers_model->add_customer($values);
                        echo $add;
                }  

如果我只是在其他部分说回声 something,则可以如果验证正常,它会回声,但是如果我在数据库中写入主题(值数组中包含数据,并且不是以ajax的方式,它会插入日期),它将无法正常工作,而else部分将无法正常工作!!!

If i just say echo "something" in the else part, It works and if the validation were OK, It echo hi but if i write theme in database (Which the value array has data and in not ajax way, it insert date), It doesn't work and the else part isn't working!!!

推荐答案

如果您提供了JS-jQuery Ajax代码,则可以更有效地理解您的问题。尝试下面的说明...

If you gave your JS- jquery Ajax code it would more efficient to understand your problem.. Don't worry! Try my following instruction...

1)获取表格的值并将其传递为

1) Get get form value and pass to form as

<script type="text/javascript"> 
  $(document).ready(function(){
    var dataString = $("#FormId").serialize();
    var url="ControllerName/MethodName"
        $.ajax({
        type:"POST",
        url:"<?php echo base_url() ?>"+url,
        data:dataString,
        success:function (data) {
            alert(data);
        }
        });     
  })
</script>

控制器:


  1. 将构造函数中的库form_validation加载为...

  1. Load library form_validation in construct as ...

$ this-> load-> library('form_validation');

$this->load->library('form_validation');

$ this-> load-> helper('form');

$this->load->helper('form');

现在编写您的控制器as ...

Now write your controller as ...

function MethodName {
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('fname','First Name', 'required');
$this->form_validation->set_rules('lname','Last Name', 'required');
$this->form_validation->set_rules('email','Email Address','required|valid_email|is_unique[sec_users.email]');
if ($this->form_validation->run() == FALSE) {
    echo validation_errors();
} 
else {
  // To who are you wanting with input value such to insert as 
  $data['frist_name']=$this->input->post('fname');
  $data['last_name']=$this->input->post('lname');
  $data['user_name']=$this->input->post('email');
  // Then pass $data  to Modal to insert bla bla!!
}

}

希望它可以在我的应用程序中正常工作。

Hope will work as it is working in my application.


请接受最好的答案。

Please accept if it is the best answer.

谢谢!

这篇关于在Codeigniter中使用jQuery Ajax进行表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆