CodeIgniter简单表单验证返回空白页

时间:2015-12-08 07:22:26

标签: php html codeigniter

我正在学习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:

enter image description here

我已尝试将网络服务器更改为XAMPP ,但仍面临同样的问题......

先谢谢你的帮助......

1 个答案:

答案 0 :(得分:0)

经过反复试验,我找到了解决方案。这是我对代码的更改:

控制器,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>