0

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

4

4 回答 4

2

您可以使用一个控制器轻松完成。我测试它并且它有效!

类名 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>
于 2013-03-01T14:40:17.600 回答
1

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

看法

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


    } 

}

?>

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

于 2013-03-01T12:17:18.677 回答
0

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

$this->from_validation->set_rules('second_name', 'Second Name', 'required|alpha|xss_clean');
于 2013-03-01T11:46:51.957 回答
0

看下面的例子,你必须在你的 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();
    }
}
于 2013-03-01T12:16:24.483 回答