dongya6381 2013-07-13 03:03
浏览 92

CodeIgniter - 无法运行$ this-> input-> post('inputName'); 在模块化扩展中

As my title, I tried call that method but I got an error:

Fatal error: Call to a member function post() on a non-object in C:\xampp\htdocs\cifirst\application\modules\front\controllers\shopping.php on line 11

If I create a controller not in module, that method I can use very easy but in this case can not (everything code in method below can not run). This is my code:

 public function add_to_cart() {
  $data = array(
   'id' => $this->input->post('productId'), // line 11
   'name' => $this->input->post('productName'),
   'price' => $this->input->post('productPrice'),
   'qty' => 1,
   'options' => array('img' => $this->input->post('productImg'))
  );

  $this->load->library('MY_Cart');
  $this->cart->insert($data);

  //redirect($_SERVER['HTTP_REFERER']);
  //echo $_POST['productId'].'-'.$_POST['productName'];
 }

And this code doesn't work too:

public function __construct() {
    $this->load->library('cart');
    $this->load->helper('form');
}

I'm using XAMPP 1.8.1, CodeIgniter 2.1.3 and newest MX. Please help me!

  • 写回答

2条回答 默认 最新

  • dsfliu1129 2013-07-13 03:33
    关注

    When you're using CodeIgniter functions outside of controllers, models, and views you need to get an instance of Codeigniter first.

    class MyClass {
        private $CI;
    
        function __construct() {
            $this->CI =& get_instance();
            $this->CI->load->library('cart');
            $this->CI->load->helper('form');
        }
    
        public function someFunction() {
            $var = $this->CI->input->post("someField");
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据