简体   繁体   English

CodeIgniter中HTML表单数据的表单验证

[英]Form validation of html form data in CodeIgniter

How can I validate the input data from a form that created without using CodeIgniter's form ? 如何在不使用CodeIgniter表单的情况下从创建的表单验证输入数据?

My form looks like this : 我的表格如下所示:

 <form method="POST" name="frmNewUser" action="<?php echo site_url('users/signup'); ?>" id="contact"> <div class="lable"> <div class="col_1_of_2 span_1_of_2"><input name="u_fname" type="text" class="text" value="First Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'First Name';}" id="active"></div> <div class="col_1_of_2 span_1_of_2"><input name="u_lname" type="text" class="text" value="Last Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Last Name';}"></div> <div class="clear"> </div> </div> <div class="lable-2"> <input name="u_email" type="text" class="text" value="your@email.com " onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'your@email.com ';}"> <input name="u_pwd" type="password" class="text" value="Password " onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Password ';}"> </div> <h3>By creating an account, you agree to our <span class="term"><a href="#">Terms & Conditions</a></span></h3> <div class="submit"> <input type="submit" name="submit" id="submit" value="Submit" > </div> <div class="clear"> </div> </form> 

And here is my controller : 这是我的控制器:

function signup(){
        $this->load->library('form_validation');

        $u_fname = $this->input->post('u_fname');
        $u_lname = $this->input->post('u_lname');
        $u_email = $this->input->post('u_email');
        $u_pwd = $this->input->post('u_pwd');

        $this->form_validation->set_rules( $this->input->post('u_fname'), 'First Name', 'required');

        if($this->form_validation->run() == FALSE){
            echo "error";
        }
    }

However the run() function of form_validation returns FALSE even though u_fname has input value. 但是,即使u_fname具有输入值,form_validation的run()函数也会返回FALSE。 How can I grab the data from the form with post method and validate the data using form_validation from CodeIgniter ? 如何使用post方法从表单中获取数据并使用CodeIgniter的form_validation验证数据?

Thank you 谢谢

Problem solved. 问题解决了。 I just need to change 我只需要改变

$this->form_validation->set_rules( $this->input->post('u_fname'), 'First Name', 'required');

to

$this->form_validation->set_rules('u_fname', 'First Name', 'required');

previous one passed the value to the function meanwhile the new one passed the "name" of the object. 前一个将值传递给函数,而新的一个则传递对象的“名称”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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