【问题标题】:Why are my CodeIgniter form values empty when form is submitted?为什么提交表单时我的 CodeIgniter 表单值为空?
【发布时间】:2016-07-19 10:25:59
【问题描述】:

在学习如何使用 Code Igniter 时,我在提交表单值时遇到了问题。如果 $_POST 不成功,我会尝试插入并重定向到表单,但是每次我提交表单时,我都会看到我的标题中没有表单值。即使在浏览了几篇相关帖子后也无法弄清楚做错了什么,希望这里的专家能提供帮助。这就是我正在使用的东西

查看: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]

表单提交期间的标题记录器

【问题讨论】:

  • 你的“csrf_token”在哪里?
  • 对不起,不知道你指的是什么。请告诉我,我是 Codeigniter 的新手

标签: php forms .htaccess codeigniter


【解决方案1】:

尝试在表单中添加此内容,

&lt;input type="hidden" name="&lt;?php echo $this-&gt;security-&gt;get_csrf_token_name(); ?&gt;" value="&lt;?php echo $this-&gt;security-&gt;get_csrf_hash(); ?&gt;"&gt;

所以你的标记现在是这样的,

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

【讨论】:

  • 感谢您的帮助。 csrf_token 是干什么用的?
  • 当您发送表单数据时,它是 CI (CodeIgniter) 的一项安全功能。这是必需的。阅读更多关于这个here.
【解决方案2】:

试试这个.. 你错过了 name 属性

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

【讨论】:

    【解决方案3】:

    你的表单应该是这样的(没有给出名称和值属性)

           <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

    【讨论】:

      【解决方案4】:

      当然,CSRF 是必需的,但我认为您的问题不同。 您错过了为每个input 标签添加name 属性。

      例如

      <input type="text" class="form-control" id="name" name="first_name" placeholder="Input field">
      

      【讨论】: