表单验证类的实现
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form','url'));
$this->load->library('form_validation');//表单验证类需先加载
$this->form_validation->set_rules('username','用户名','callback_username_check|min_length|max_length');
$this->form_validation->set_rules('password','密码','required|matches');
$this->form_validation->set_rules('repassword','确认密码','required');
$this->form_validation->set_rules('email','邮件','required|valid_email');
if($this->form_validation->run()==FALSE)
{
$this->load->view('myheader');
/*$this->load->helper('form');
echo form_open('');*/
$this->load->view('myform');
$this->load->view('myfooter');
}
else
{
$this->load->view('formsuccess');
}
}
public function username_check($str)
{
if($str=='aaaaa')//检查输入的用户名是否存在
{
$this->form_validation->message('username_check','%s用户名已存在!');
}
else
{
return TRUE;
}
}
}
正确输入后怎么不能跳转到formsuccess.php页面(要么无法显示,要么正在连接...) 页面无法显示?那要看到底是什么错误。
不过可以肯定是你的代码或者服务器问题。 回复 2# Hex
能帮我分析分析吗?myform.php和formsuccess.php两个视图文件的代码都在这...
//myform.php
<?php echo validation_errors();?><!-- 函数将会返回验证器送回的所有错误信息。如果没有错误信息,它将返回空字符串。 -->
<br>
<?php echo form_open('form');?><!-- 添加uri片段 -->
<!-- <form action="" name="login" method="post">-->
<h5>用户名</h5>
<input type="text" name="username" value="<?php echo set_value('username');?>">
<h5>密码</h5>
<input type="password" name="password" value="<?php echo set_value('password');?>">
<h6>确认密码</h6>
<input type="password" name="repassword" value="<?php echo set_value('repassword');?>">
<h5>邮件</h5>
<input type="text" name="email" value="<?php echo set_value('email');?>">
<input type="submit" value="确定">
</form>
//formsuccess.php:
<html>
<head>
<title>登录成功</title>
</head>
<body>
<h3>恭喜你,登录成功</h3>
</body>
</html> $this->form_validation->set_message('username_check','%s用户名已存在!');
return FALSE; 回复 4# qi_ruo
貌似还不是这里的问题,今天把views里面myform.php的<?php echo form_open(form');?>语句改成了<?php echo form_open('http://localhost:8080/CodeIgniter_2.0.1/index.php/form/username_check');?>就报错了:1,Missing argument 1 for Form::username_check()
。2,Undefined variable: str。不知道该如何把这个参数传递过去。而且我把username_check函数的if语句删除,直接加上一条$this->load->view('form/formsuccess');就可以直接跳转了(但是验证规则没起作用,当我什么都没输入的时候单击确定它也会跳转到指定页面),所以我怀疑这个函数的写法,还请指教!!!
还有一个问题,form_open语句,没改这条语句之前单击提交按钮后url为localhost:8080/index.php/form
改了之后url为http://localhost:8080/CodeIgniter_2.0.1/index.php/form/username_check
CodeIgniter_2.0.1怎么会消失??? 表单验证的自定义函数不是做为表单的action的,它会由form_validation类自己调用并检查是否通过验证,所以之前的那个return FALSE;是一定要有的,否则form_validation会认为通过了验证;
form_open会自动加上site_url的,既然那个表单的url不对,说明base_url和site_url设置有问题,你可以打印base_url和site_url检查一下; 回复 6# qi_ruo
嗯,第二个问题解决了,改一下配置文件的base_url就可以了。但还有一个老问题-参数错误:1,Missing argument 1 for Form::username_check()
2,Undefined variable: str
根本就执行不到你说的return FALSE语句。 参数的问题,请参考手册:
添加一些属性
可以在第二个参数里传递一个关联数组来达到这一目的, 像这样:
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);
上面的例子会创建一个这样的form标签:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send"class="email"id="myform" />
你如果把form_open(form');改成 form_open('form/username_check');当然会报这个错了,因为没有提供username_check函数的参数;
form_open('form') 是对的啊,为什么要改它呢? 回复 8# rockics
那怎么把在用户名里输入的值传递给username_check函数
页:
[1]
2