【问题标题】:codeigniter form validation not functioningcodeigniter 表单验证不起作用
【发布时间】:2013-03-01 11:43:45
【问题描述】:

我正在开发一个小型表单,它将数据汇总到一个表中,再放到一个数据库中,这没什么了不起的。我正在尝试添加一些验证规则,但它们不起作用我仍然是 php 和 codeigniter 的初学者,所以无法弄清楚为什么有人可以查看我的代码并提前帮助我解决 tnx。

查看

<html>
    <head>
    </head> 
 <body>
  <?php echo validation_errors(); ?>
   <?php echo form_open('http://localhost/Surva/index.php/info/credentials/'); ?>
     <ul id="info">  
       <li><label for='name'>Name:</label><?php echo form_input('name')?>
      </li>

       <li><label for='second_name'>Second Name:<label> <?php echo form_input('second_name');?>
     </li>

       <li><label fro='phpne'>Phone:</label> <?php echo form_input('phone');?></li>

       <li><label for='email'>Email: </label><?php echo form_input('email');?>
       </li>

       <li><?php echo form_submit('submit', 'Start survay!!' );?></li>
     </ul>  

 <?php echo form_close();?>
  </body>
</html>

控制器

<?php

class Info extends CI_Controller{

    function index(){

      $this->load->view('info_view');   
    }

    function credentials()
    {   
     $data = array(
         'name' => $this->input->post('name'),
         'second_name' => $this->input->post('second_name'),
         'phone' => $this->input->post('phone'),
         'email' => $this->input->post('email'),  
         );


          $this->info_model->add_record($data);
    }    

    function validation(){

        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Name', 'required|alpha|xss_clean');
        $this->from_validation->set_rules('second_name', 'required|alpha|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

        if ($this->form_validation->run() == FALSE){


            $this->load->view('info_view');

        }else{
            redirect('survaycontroller/index');
        }


    } 

}

?>

我使用 codeigniter 用户指南进行验证,那里的解释看起来很简单,整个结构取自指南,我很难理解问题所在。

【问题讨论】:

    标签: php forms codeigniter


    【解决方案1】:

    您可以使用一个控制器轻松完成。我测试了它,它可以工作!

    类名 test.php

    控制器名称索引

    function index(){
    
            $this->load->library('form_validation');
    
            $data['name']           = $this->input->post('name');
            $data['second_name'] = $this->input->post('second_name');
            $data['phone']          = $this->input->post('phone');
            $data['email']              = $this->input->post('email');
    
            if($this->input->post('submit')) {
    
                $this->form_validation->set_rules('name', 'Name', 'required|alpha|xss_clean');
                $this->form_validation->set_rules('second_name', 'Second Name', 'required|alpha|xss_clean');
                $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    
                if ($this->form_validation->run()){
    
                    $this->info_model->add_record($data);
    
                }
    
            }
            $this->load->view('test');
        }
    

    查看:test.php

    <html>
        <head>
        </head> 
     <body>
      <?php echo validation_errors(); ?>
       <?php echo form_open('test/index'); ?>
         <ul id="info">  
           <li><label for='name'>Name:</label><?php echo form_input('name')?>
          </li>
    
           <li><label for='second_name'>Second Name:<label> <?php echo form_input('second_name');?>
         </li>
    
           <li><label fro='phpne'>Phone:</label> <?php echo form_input('phone');?></li>
    
           <li><label for='email'>Email: </label><?php echo form_input('email');?>
           </li>
    
           <li><?php echo form_submit('submit', 'Start survay!!' );?></li>
         </ul>  
    
     <?php echo form_close();?>
      </body>
    </html>
    

    【讨论】:

    • tnx 寻求帮助
    • 我试过你的解决方案它不起作用我得到的只是一个错误页面没有找到并且没有显示验证消息现在它没有添加到数据库中。
    【解决方案2】:

    您没有将表单发送到方法验证

    查看

    <html>
        <head>
        </head> 
     <body>
      <?php echo validation_errors();
       //you must redirect the form to the right method, in this case your method is "validation" on the controller is "info"
       //another thing, you don't need to put all the url, just put the controller and the method, this way when you migrate your website to a server you don't have to worry changing the url
       echo form_open('info/validation'); ?>
         <ul id="info">  
           <li><label for='name'>Name:</label><?php echo form_input('name')?>
          </li>
    
           <li><label for='second_name'>Second Name:<label> <?php echo form_input('second_name');?>
         </li>
    
           <li><label fro='phpne'>Phone:</label> <?php echo form_input('phone');?></li>
    
           <li><label for='email'>Email: </label><?php echo form_input('email');?>
           </li>
    
           <li><?php echo form_submit('submit', 'Start survay!!' );?></li>
         </ul>  
    
     <?php echo form_close();?>
      </body>
    </html>
    

    控制器

    <?php
    
    class Info extends CI_Controller{
        //I've added the public before function
        public function index(){
    
          $this->load->view('info_view');   
        }
        //In this one I've added private, Why? Because you don't peopple to use this method if they go to http://(yourdomain). This way you can only use this function inside this controller
        private function credentials()
        {   
         $data = array(
             'name' => $this->input->post('name'),
             'second_name' => $this->input->post('second_name'),
             'phone' => $this->input->post('phone'),
             'email' => $this->input->post('email'),  
             );
    
    
              $this->info_model->add_record($data);
        }    
        //this one is also as public, and it's the one who we'll receive the post request from your form
        public function validation(){
    
            $this->load->library('form_validation');
            $this->form_validation->set_rules('name', 'Name', 'required|alpha|xss_clean');
            $this->from_validation->set_rules('second_name', 'required|alpha|xss_clean');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    
            if ($this->form_validation->run() == FALSE){
    
    
                $this->load->view('info_view');
    
            }else{
                //if the form is valid then you call the private function credentials and save the data to your database
                $this->credentials();
                redirect('survaycontroller/index');
            }
    
    
        } 
    
    }
    
    ?>
    

    检查我对您的代码所做的更改,如果您需要更多解释或帮助,欢迎您

    【讨论】:

    • tnx 为您的帮助,我将代码作为您的示例进行了编辑,但出现页面未找到错误,并且除了未找到页面外,没有显示与以前相同的验证规则。有什么建议吗?
    • 你能把找不到页面的网址贴出来吗?
    • 另一件事我注意到你没有加载模型,在凭证函数上将它添加到第一行 $this->load_model('info_model');网址是正确的,它不应该给你找不到页面。你在 routes.php 文件上配置了什么
    • 不好说。在根页面上,将 default_controller 更改为登录,这样就不再进行配置了。
    • 我担心需要加载模型,因为存储数据的功能正在工作,但无论如何我都会把它放进去。
    【解决方案3】:

    第二条规则中缺少第二个参数

    $this->from_validation->set_rules('second_name', 'Second Name', 'required|alpha|xss_clean');
    

    【讨论】:

      【解决方案4】:

      看下面的例子,你必须在你的 credentials() 函数中检查 $this->form_validation->run()。

      public function add_category() 
      {
          $this->load->library('form_validation');
      
          $data = array();
      
          if ($_SERVER['REQUEST_METHOD'] == 'POST')
          {
              //Validation rules
              $this->form_validation->set_rules('name', 'Name', 'trim|required');;
              $this->form_validation->set_rules('description', 'Description', 'trim|required');   
              $this->form_validation->set_rules('position', 'Position', 'trim|numeric');      
      
      
              $artist_category = $this->input->post('artist_category', TRUE);
              $data['artist_category'] = $artist_category;
      
              if($this->form_validation->run() === FALSE)
              {
                  $this->template->write('title', 'Category : Add Category');
                  $this->template->write_view('content', 'category/add_category',$data);
                  $this->template->render();
              }
              else
              {
                  $name = trim($this->input->post('name',TRUE));
                  $description = trim($this->input->post('description',TRUE));
                  $position = trim($this->input->post('position',TRUE));
                  $colour = trim($this->input->post('colour',TRUE));
      
                  $language_id = trim($this->input->post('language_id',TRUE));
      
      
                  //Add record to categories table
                  $category_id = $this->Category_model->insert_category($name,$description,$position,$colour,$date_added);
      
      
      
                  $this->session->set_flashdata('success_msg', 'Category added successfully.');
      
                   //Redirect to manage categories
                   redirect('category');
               }              
      
      
          }
          else
          {          
              $this->template->write('title', 'Category : Add Category');
              $this->template->write_view('content', 'category/add_category');
              $this->template->render();
          }
      }
      

      【讨论】:

      • 所以我必须将凭据和验证功能作为一个功能?
      • 是的,我们就是这样做的。如果不传递 $_POST 变量,我认为您的方式不会起作用
      • CI 文档中是这样解释的
      • 试试看它会起作用。不要忘记标记对您有帮助的答案。
      猜你喜欢
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2020-09-28
      • 2012-02-26
      • 1970-01-01
      • 2015-01-20
      相关资源
      最近更新 更多