Codeigniter表单验证不显示错误并重定向到空白页面

时间:2015-11-17 06:34:08

标签: php codeigniter validation

以下是控制器和视图,但未经过验证,仅重定向到about:blank page。 我做了一些改变但没有任何反应。

控制器:

<?php
    class Home extends CI_Controller {

    public function __construct() {
    parent::__construct();
    $this->load->library(array('form_validation','session')); // load form     lidation libaray & session library
$this->load->helper(array('url','html','form'));  // load url,html,form helpers optional
}

public function index(){
// set validation rules
$this->form_validation->set_rules('name', 'Name', 'required|min_length[4]|max_length[10]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Phone Number', 'required|numeric|max_length[15]');
$this->form_validation->set_rules('subject', 'Subject', 'required|max_length[10]|alpha');
$this->form_validation->set_rules('message', 'Message', 'required|min_length[12]|max_length[100]');

// hold error messages in div
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
// check for validation
if ($this->form_validation->run() == FALSE) {
$this->load->view('viewform');
}else{
$this->session->set_flashdata('item', 'form submitted successfully');
redirect('Home');
}
}
}
?>

视图:

<?php if(validation_errors()) { ?>
    <div class="alert alert-warning">
    <?php echo validation_errors(); ?>
    </div>
    <?php } ?>


    <?php if($this->session->flashdata('item')) { ?>
    <div class="alert alert-success">
    <?php echo $this->session->flashdata('item'); ?>
    </div>
    <?php } ?>


    <?php echo form_open(); ?>

      <div class="form-group">
        <?php echo form_label('Your Name','name'); ?>
        <?php echo form_input(array("class"=>"form-control","name" => "name", "placeholder"=>"Enter Name","value" => set_value('name'))); ?>
      </div>

      <div class="form-group">
        <?php echo form_label('Email address','EmailAddress'); ?>
        <?php echo form_input(array("class"=>"form-control","name" => "email", "placeholder"=>"Enter email","value" => set_value('email'))); ?>
      </div>

      <div class="form-group">
        <?php echo form_label('Phone Number','number'); ?>
        <?php echo form_input(array("class"=>"form-control","name" => "number", "placeholder"=>"Enter Phone Number","value" => set_value('number'))); ?>
      </div>

       <div class="form-group">
        <?php echo form_label('Subject','subject'); ?>
        <?php echo form_input(array("class"=>"form-control","name" => "subject", "placeholder"=>"Enter Subject","value" => set_value('subject'))); ?>
      </div>

       <div class="form-group">
        <?php echo form_label('Message','message'); ?>
        <?php echo form_input(array("class"=>"form-control","name" => "message", "placeholder"=>"Enter Message","value" => set_value('message'))); ?>
      </div>

      <button type="submit" class="btn btn-default">Submit</button>
    <?php echo form_close(); ?>

所以当出现任何错误时,页面会被重定向到about:blank,即使所有字段都正确或不正确如何解决这个问题?

0 个答案:

没有答案