简体   繁体   English

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

[英]PHP Code Igniter Form Validation Not Working

I'm using PHP Code Igniter and am having trouble with a form validation. 我正在使用PHP Code Igniter并且在表单验证方面遇到问题。 I followed the form validation tutorial on Code Igniter but it is not working. 我按照Code Igniter上的表单验证教程进行了操作,但它不起作用。 Empty fields are submitted to the database. 空字段将提交到数据库。

I added the form validation rule sets to the index function in the controller. 我将表单验证规则集添加到控制器中的索引函数。

Controller: 控制器:

   <?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();
  }

}

And the View file. 和View文件。

<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>

If you compare your code here and basic example in CI userguide, you can see those two are not similar examples. 如果您将此处的代码与CI用户指南中的基本示例进行比较,您可以看到这两个代码不是类似的示例。 You missed to load session library in array passed in constructor. 您错过了在构造函数中传递的数组中加载会话库。 Feel free to remove loading libraries and helpers from other methods (ie index ) because those are loaded in constructor already and respective objects/functions are available in all class methods after. 随意从其他方法(即index )中删除加载库和帮助程序,因为它们已经在构造函数中加载,并且各个对象/函数在之后的所有类方法中都可用。 Make array of public variable, var is deprecated keyword. 创建公共变量数组, 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();
    }
}

Put <?= form_open('phonebook') ?> in form. <?= form_open('phonebook') ?>放在表格中。 Also, fix set_rules() . 另外,修复set_rules() I just copied/pasted yours but not seem right. 我只是复制/粘贴你的,但似乎不对。 Read name of fierlds and coresponding rules again in this code - doesn't match. 在此代码中再次读取fierlds和相应规则的名称 - 不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM