【问题标题】:Submit button not working when inserting form value插入表单值时提交按钮不起作用
【发布时间】:2017-10-05 05:51:45
【问题描述】:

控制器: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 消息,不知道为什么。那么,我该如何解决这个问题?请帮帮我。

谢谢

【问题讨论】:

  • 您需要在表单标签中添加操作。
  • 您是否收到任何错误或什么?
  • 在表单标签@omkara 中添加操作方法
  • 不,我没有收到任何错误@NaimMalek

标签: php forms codeigniter


【解决方案1】:

这一行的问题。当表单为submit时,您需要添加action

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

【讨论】:

  • 用您的代码替换上面的行并清除浏览器缓存然后提交表单
  • 点击提交按钮会发生什么?
  • 当点击提交按钮时,它没有显示任何类似 flashdata 或不插入表单值
  • @omkara 我已经更新了我的答案试试这个并显示结果
【解决方案2】:

您需要在表单标签中添加操作方法。

<form role="form" method="post" action="<?php echo site_url('purchase/add_product_master');?>"> 

【讨论】:

  • 你添加action方法了吗?提交按钮在应用操作方法时起作用
  • 提交按钮在应用操作方法时起作用是错误的陈述。如果请求在相同的 url/页面上发送,则不需要 action
  • @B.Desai 感谢您的指正。尝试将 if($this->input->post('submit')) 更改为 if (!empty($_POST))
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-10
    • 2016-09-18
    • 2022-08-17
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多