PHP代码点火器表单验证不起作用

时间:2016-03-20 23:33:55

标签: php forms codeigniter validation

我正在使用PHP Code Igniter并且在表单验证方面遇到问题。我按照Code Igniter上的表单验证教程进行了操作,但它不起作用。空字段将提交到数据库。

我将表单验证规则集添加到控制器中的索引函数。

控制器:

   <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Phonebook extends CI_Controller {

  var $TPL;

  public function __construct()
  {
    parent::__construct(); 

    $this->TPL['newentry'] = false;
    $this->load->library(array('form_validation')); // load form lidation libaray & session library
    $this->load->helper(array('url','form'));  // load url,html,form helpers optional
  }

   public function index()
  {     
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $TPL->form_validation->set_rules('fname', 'First Name', 'required|min_length[5]|max_length[12]|is_unique[phonebook.fname]');
    $this->form_validation->set_rules('lname', 'Last Name', 'required|matches[passconf]');
    $this->form_validation->set_rules('phone', 'Password Confirmation', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[phonebook.email]');

    if ($this->form_validation->run() == FALSE)
    {
      $this->load->view('phonebook_view');
    }
    else
    {
      $this->load->view('success_message');
    }
  }

  public function newentry()
  { 
    $fname = $this->input->post("fname");
    $lname = $this->input->post("lname");
    $phone = $this->input->post("phone");
    $email = $this->input->post("email");
    $query = $this->db->query("INSERT INTO phonebook VALUES (NULL, '$fname', '$lname', '$phone', '$email', NULL, NULL, NULL, NULL);");

    $this->display(); 
  }

  public function addnew()
  {
    $this->TPL['newentry'] = TRUE;

    $this->display();
  }

}

查看文件。

<html>
<body>
<h1><a href="<?= base_url()?>">Phonebook</a></h1>

<? if ($newentry) { ?>

<?= form_open('Phonebook/newentry') ?>
<?= form_fieldset("Add Entry") ?>
<?= form_label('First Name:', 'fname'); ?> <br>
<?= form_input(array('name' => 'fname',
 'id' => 'fname')); ?> <br>
<?= form_label('Last Name:', 'lname'); ?> <br>
<?= form_input(array('name' => 'lname', 
 'id' => 'lname')); ?> <br>
<?= form_label('Phone Number:', 'phone'); ?> <br>
<?= form_input(array('name' => 'phone',
 'id' => 'phone')); ?> <br>
<?= form_label('E-mail:', 'email'); ?> <br>
<?= form_input(array('name' => 'email',
 'id' => 'email')); ?> <br>
<?= form_submit('phonebooksubmit', 'Submit'); ?>
<?= form_fieldset_close(); ?>
<?= form_close() ?>

<? } else { ?>

<p><a href="<?= base_url() ?>index.php?/Phonebook/addnew">Add new entry</a></p>

<? } ?>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

如果您将此处的代码与CI用户指南中的基本示例进行比较,您可以看到这两个代码不是类似的示例。您错过了在构造函数中传递的数组中加载会话库。随意从其他方法(即index)中删除加载库和帮助程序,因为它们已经在构造函数中加载,并且各个对象/函数在之后的所有类方法中都可用。创建公共变量数组,var是不推荐使用的关键字。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Phonebook extends CI_Controller
{

    public $TPL = array();

    public function __construct()
    {
        parent::__construct(); 

        $this->TPL['newentry'] = false;
        $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()
    {
        $this->form_validation->set_rules('fname', 'First Name', 'required|min_length[5]|max_length[12]|is_unique[phonebook.fname]');
        $this->form_validation->set_rules('lname', 'Last Name', 'required|matches[passconf]');
        $this->form_validation->set_rules('phone', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[phonebook.email]');

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('phonebook_view');
    }
    else
    {
        $fname = $this->input->post("fname");
        $lname = $this->input->post("lname");
        $phone = $this->input->post("phone");
        $email = $this->input->post("email");
        $query = $this->db->query("INSERT INTO phonebook VALUES (NULL, '$fname', '$lname', '$phone', '$email', NULL, NULL, NULL, NULL);");

        if ((int)$this->db->affected_rows() < 1)
        {
            print_r($this->db->error());
            exit;
        }
        else
        {
            redirect('success_page', 'refresh');
            //maybe? $this->display();
        }
    }

    public function addnew()
    {
        $this->TPL['newentry'] = TRUE;

        $this->display();
    }
}

<?= form_open('phonebook') ?>放入表单中。另外,修复set_rules()。我只是复制/粘贴你的,但似乎不对。在此代码中再次读取fierlds和相应规则的名称 - 不匹配。