php - 无法在 codeigniter Controller 中获取 POST 数据

标签 php codeigniter post

<分区>

出于某种原因,我似乎无法在 codeigniter Controller 中发布数据。我把它分解成一个非常简单的形式来测试它,仍然没有运气。如果我使用 method="get"它工作正常。不管怎样,下面是表单、 Controller /函数和我的 .htaccess。任何帮助将非常感激。此外,我在这里看到了其他一些类似的问题,但似乎没有一个答案适合我。

形式:

<form id="bundleOrderInfo" name="bundleOrderInfo" action="<?php echo(base_url()); ?>catalog/bundleSubmit" method="post"> 

<input type="text" name ="test" id="test" value="blahblah"></input>
<input type="submit"></input>
</form>

Controller /函数:

    public function bundleSubmit()
{
   $this->output->enable_profiler();
   $this->load->model('catalog_model');

   $data['availableCategories']=$this->catalog_model->getCategories();
   $data['availableItems'] = $this->catalog_model->getByCategory($data['availableCategories']);
   $testing = $this->catalog_model->formData();

   $this->load->view('templates/header');
   $this->load->view('templates/menu',$data);

   print_r($_POST);
}

.htaccess:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /

   RewriteCond %{REQUEST_URI} ^system.*
   RewriteRule ^(.*)$ /ITPortal/index.php?/$1 [L]

   RewriteCond %{REQUEST_URI} ^application.*
   RewriteRule ^(.*)$ /index.php?/$1 [L]

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ ITPortal/index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /ITPortal/index.php
</IfModule> 

最佳答案

action 应该指向 controller 函数,如果你尝试 FormHelper 你的生活会容易得多

https://www.codeigniter.com/user_guide/helpers/form_helper.html

尝试在构造函数 [__construct() 函数] 加载模型、助手、库,这是一个很好的正确方法。

Controller

function __construct()
{
     parent:: __construct();
     $this->load->helper('form'); //loading form helper
     $this->load->model('catalog_model'); //loading your model
}

function bundleSubmit()
{
     $this->catalogmodel->insertFromForm();  //calling your method from model
}

通常你应该在模型中捕获Posted Value

型号

  function insertFromForm()
  {
     $name= $this->input->post('name');
     print_r($name);
     die(); // stop the process here
  }

查看

<?php echo form_open('catalog/bundleSubmit','id="bundleOrderInfo" name="bundleOrderInfo"') ;?>
//you can also do this , this should be enough 
//<?php echo form_open('catalog/bundleSubmit')?>

<input type="text" name ="test" id="test" value="blahblah"></input>
<input type="submit" value="Submit"></input>

<?php echo form_close();?>

加法

'catalog/BundleSubmit' in form mean means your form posted values will goes to to 'controller/function()' and controller will redirect to a method in model 'model/insertDataFromForm"

如果你想了解更多可以查看CI的目录

事情是如何运作的

https://www.codeigniter.com/user_guide/overview/appflow.html

更多信息:- https://www.codeigniter.com/user_guide/

关于php - 无法在 codeigniter Controller 中获取 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266512/

相关文章:

php - 如何获取当前日期与数据库中的日期之间的差异并将其显示在表中

php - MySQL INSERT 未使用 PHP 插入预期值

php - Windows 和 Linux 上 mysqldump 的路径

php - 如何在Codeigniter中创建两个父 Controller ?

javascript - 使用名为 submit 的输入使用 Javascript 提交表单

javascript - 如何将ajax响应值传递到另一个页面?

javascript - 使用 Jquery if 和 else 函数禁用按钮

php - 从 .CSV 文件比较/插入/更新 MySQL 数据库中的产品的最佳方法是什么

web-services - 在 RESTful 服务中部分更新复杂类型

c# - WebMatrix 的 WebImage Helper 支持哪些图像文件类型?