php 无法在 codeigniter 控制器中获取 POST 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16266512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:48:58  来源:igfitidea点击:

Cant get POST data in codeigniter controller

phpcodeigniterpost

提问by Mike

For some reason I cant seem to get to post data in a codeigniter controller. I broke it down to a very simple form to test it out, still no luck. If I use method="get" it works fine. Anyways, below is the form, the controller/function, and my .htaccess. Any help would be much appreciated. Also, I saw a few other similar questions on here, but none of them seemed to have an answer that worked for me.

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

form:

形式:

<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/function:

控制器/功能:

    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:

.htaccess:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /

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

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

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

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

回答by Wayne Tun

action should direct to controller function, if you try FormHelper your life will be much easier

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

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

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

Try to load models,helpers, libraries at constructor [ __construct() function] it is a good proper way.

尝试在构造函数 [ __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
}

Normally you should catch Posted Value in model

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

Model

模型

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

View

看法

<?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();?>

Addition

添加

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

If you want to know more you can check with CI's table of contents

如果您想了解更多信息,可以查看 CI 的目录

How things work

如何工作

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

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

more info:- https://www.codeigniter.com/user_guide/

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