繁体   English   中英

Codeigniter表单验证不起作用

[英]codeigniter form validation not functioning

我正在以一种小型表格的形式工作,将数据放到表上再放到数据库中,这没什么特别的。 我正在尝试添加一些验证规则,但是它们不起作用,我仍然是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用户指南进行验证,其中的解释看起来非常容易,整个结构取自该指南,我很难理解问题所在。

您可以使用一个控制器将其简化。 我对其进行了测试并成功!

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

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

视图

<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');
        }


    } 

}

?>

如果您需要更多说明或欢迎您,请查看我对您的代码所做的更改

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

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

看下面的示例,您必须在凭据()函数中检查$ 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();
    }
}

暂无
暂无

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

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