CodeIgniter 简单表单验证返回空白页
Posted
技术标签:
【中文标题】CodeIgniter 简单表单验证返回空白页【英文标题】:CodeIgniter Simple Form Validation Return Blank Page 【发布时间】:2016-03-13 00:52:16 【问题描述】:我正在学习 php Codeigniter,尤其是在表单验证方面。我所有的源代码都是从their website 复制粘贴的,没有任何改变(只是复制粘贴)。这个表单验证看起来很简单,但是每次我点击“提交按钮”然后出现空白页。代码是:
MyForm.php (查看)
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
FormSuccess.php(成功页面):
<html>
<head>
<title>My Form</title>
</head>
<body>
<h3>Your form was successfully submitted!</h3>
<p><?php echo anchor('form', 'Try it again!'); ?></p>
</body>
</html>
和Form.php(控制器):
<?php
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', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
$this->load->view('MyForm');
else
$this->load->view('FormSuccess');
如果我的代码有效,在我点击提交按钮后,它应该会转到成功页面(formsuccess.php),但现在它返回空白页面。
SS 表格:
我尝试将 webserver 更改为 XAMPP,但仍然遇到同样的问题...
感谢您的帮助...
【问题讨论】:
你是从服务器运行 php,比如 wamp? @Victoria 是的,我正在使用 wamp 运行 php... 把你的文件名改成form.php
如果你使用 CI 是哪个版本??
@Saty,我试过了,但还是失败了。我正在使用 CI 3.0 和 WAMP 2.5
【参考方案1】:
经过反复试验,我找到了解决方案。这是我对代码的更改:
控制器,Form.php
<?php
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', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
$this->load->view('myform');
else
$this->load->view('formsuccess');
?>
查看,MyForm.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<form action="<?php base_url();?>form" method="POST">
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
【讨论】:
以上是关于CodeIgniter 简单表单验证返回空白页的主要内容,如果未能解决你的问题,请参考以下文章
在 Codeigniter 中填充表单数据和验证数据的更简单方法?