插入表单值

时间:2017-10-05 05:51:46

标签: php forms codeigniter

controller:Purchase.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Purchase extends CI_Controller 
{
    function __construct()
    {
        parent:: __construct();
        $this->load->model('purchase_data');
    }
    public function add_product_master()
    {
        if($this->input->post('submit'))
        {
            $data = array(
                        'product_name'=> $this->input->post('product_name'),
                        'category'=> $this->input->post('category'),
                        'sub_category'=> $this->input->post('sub_category'),
                        'description'=> $this->input->post('description'),
                        );
            $query = $this->db->insert('product_master',$data); 
            if($query == true)
            {
                $this->session->set_flashdata('message', '<p style="color: green;font-weight: bold;">Your product added successfully.</p>');
                echo "<meta http-equiv='refresh' content='1'>";
            }
            else
            {
                $this->session->set_flashdata('message', '<p style="color: red;font-weight: bold;">Error!</p>');
            }
        }
        $this->load->view('product-master');
    }
}

查看:product-master.php

<?php echo $this->session->flashdata('message');?>
<form class="form-horizontal form-label-left" method="post">
  <input type="text" name="product_name" id="product_name" required="required" />
  <input type="text" name="category" id="category" required="required" />
  <input type="text" name="sub_category" id="sub_category" required="required" />
  <textarea name="description" id="description" required="required"></textarea>
  <input type="submit" name="submit" id="submit" class="btn btn-success" value="submit">
</form>

我创建了一个名为product-master.php的表单。但是,当我点击提交按钮时,它不会插入表单值或不显示任何flashdata消息不知道为什么。那么,我该如何解决这个问题呢?请帮帮我。

谢谢

3 个答案:

答案 0 :(得分:1)

这一行的问题。表格为action

时,您需要添加submit
 <form class="form-horizontal form-label-left" method="post"
 action="<?php echo site_url('purchase/add_product_master');?>">

在此函数开头的函数add_product_master中使用此代码

echo "<pre>";
print_r($this->input->post());
exit;

答案 1 :(得分:0)

您需要添加操作方法以形成标记。

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

答案 2 :(得分:0)

表单中缺少action属性,因此您应该在向表单添加表单时添加action属性。

<form class="form-horizontal form-label-left" method="post" action="URL">