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

コントローラー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>
于 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

以下の例を見てください。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 に答える
0

2 番目のルールに 2 番目のパラメータがありません

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