提交表单时,为什么我的CodeIgniter表单值为空?

时间:2016-07-19 10:26:00

标签: php forms .htaccess codeigniter

在学习如何使用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>

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

4 个答案:

答案 0 :(得分:2)

试试这个..你错过了名称属性

<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 :(得分: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>

答案 2 :(得分:1)

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

       <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

答案 3 :(得分:1)

当然,CSRF将是必需的,但我认为你的问题是不同的。 您错过了为每个name代码添加input媒体资源。

<强> e.g

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