2016-07-19 3 views
0

Code Igniterの使い方を学びながら、フォーム値を送信するときに問題が発生しました。私は$ _POSTの挿入をしようとしていて、それが成功しなかった場合はフォームにリダイレクトしますが、フォームを送信するたびに私のヘッダーにはフォームの値がないことがわかります。いくつかの関連記事を経ても何が間違っているのか分からず、ここの専門家の助けに感謝します。ここで私はフォームが送信されるときに私のCodeIgniterフォームの値が空白になるのはなぜですか?

ビューで働いているものです:new_product.php

<form action="products/new_product" method="POST" role="form"> 
    <legend>Upload New Item</legend> 

    <div class="form-group"> 
     <label for="">Name</label> 
     <input type="text" class="form-control" id="name" placeholder="Input field"> 
     <label for="">Description</label> 
     <input type="text" class="form-control" id="description" placeholder="Input field"> 
     <label for="">Category</label> 
     <input type="text" class="form-control" id="category" placeholder="Input field"> 
     <label for="">Price</label> 
     <input type="text" class="form-control" id="price" placeholder="Input field"> 
    </div> 

    <button type="submit" class="btn btn-primary">Upload</button> 
</form> 

コントローラー:products.php

public function new_product() 
{ 
    if ($_POST) { 
     $data = array(
      'category' => $this->input->post('category'), 
      'name' => $this->input->post('name'), 
      //'photo' => $this->input->post('photo'), 
      'description' => $this->input->post('description'), 
      'price' => $this->input->post('price') 
     ); 
     $this->product->upload_product($data); 
     redirect(base_url().'products/'); 
    } else { 
     $this->load->view('header', FALSE); 
     $this->load->view('new_product'); 
     $this->load->view('footer', FALSE); 
    } 
} 

モデル:product.php

function upload_product($data) { 
    $this->db->insert('products', $data); 
    return $this->db->insert_id(); 
} 

の.htaccess

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 

フォーム送信時のヘッダレコーダー

header information

+0

? – rmondesilva

+0

申し訳ありませんが、あなたが何を指しているのか分かりません。私と一緒に裸にしてください、私はCodeigniterの新人です – Mena

答えて

1

フォーム内にこれを追加してみてください、

<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">

だからあなたのマークアップは次のようになり、

<form action="products/new_product" method="POST" role="form"> 
    <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>"> 
    <legend>Upload New Item</legend> 

    <div class="form-group"> 
     <label for="">Name</label> 
     <input type="text" class="form-control" id="name" placeholder="Input field"> 
     <label for="">Description</label> 
     <input type="text" class="form-control" id="description" placeholder="Input field"> 
     <label for="">Category</label> 
     <input type="text" class="form-control" id="category" placeholder="Input field"> 
     <label for="">Price</label> 
     <input type="text" class="form-control" id="price" placeholder="Input field"> 
    </div> 

    <button type="submit" class="btn btn-primary">Upload</button> 
</form> 
+0

助けを感謝します。 csrf_tokenとは何ですか? – Mena

+0

フォームデータを送信しているときに、CI(CodeIgniter)のセキュリティ機能です。それは必須です。これについての詳細はこちら[ここをクリック](https://www.codeigniter.com/user_guide/libraries/security.html) – rmondesilva

+0

ありがとう – Mena

1

あなたのフォームshou ldは、この(NO名前と値の属性与えられる)

 <form action="products/new_product" method="POST" role="form"> 
       <legend>Upload New Item</legend> 

      <div class="form-group"> 
       <label for="">Name</label> 
        <input type="text" name="name" value="" class="form-control" id="name" placeholder="Input field"> 
        <label for="">Description</label> 
        <input type="text" name="description" value="" class="form-control" id="description" placeholder="Input field"> 
        <label for="">Category</label> 
       <input type="text" name="category" value="" class="form-control" id="category" placeholder="Input field"> 
       <label for="">Price</label> 
        <input type="text" name="price" value="" class="form-control" id="price" placeholder="Input field"> 
      </div> 

      <button name='submit' type="submit" class="btn btn-primary">Upload</button> 
    </form> 

http://www.w3schools.com/html/html_forms.asp

2

llike ..あなたは勿論、CSRFが必要となります

<form action="products/new_product" method="POST" role="form"> 
    <legend>Upload New Item</legend> 

    <div class="form-group"> 
     <label for="">Name</label> 
     <input type="text" class="form-control" name="name" id="name" placeholder="Input field"> 
     <label for="">Description</label> 
     <input type="text" class="form-control" name="description" id="description" placeholder="Input field"> 
     <label for="">Category</label> 
     <input type="text" class="form-control" name="category" id="category" placeholder="Input field"> 
     <label for="">Price</label> 
     <input type="text" class="form-control" id="price" name="price" placeholder="Input field"> 
    </div> 

    <button type="submit" class="btn btn-primary">Upload</button> 
</form> 
1

属性を逃したが、あなたのこれを試してみてくださいします問題は違うと思う。 あなたのinputタグのそれぞれにnameプロパティを追加していません。あなたの 'csrf_token' は

例えば

<input type="text" class="form-control" id="name" name="first_name" placeholder="Input field"> 
関連する問題