【问题标题】:Data not passing from form to controller in CodeIgniter数据未在 CodeIgniter 中从表单传递到控制器
【发布时间】:2016-05-02 13:00:08
【问题描述】:

我想将数据从表单传递到我的控制器,然后我想从那里将​​它们保存到我的数据库中,但是当我尝试将数据保存在控制器中时,数组是空的。

查看页面

<form method="post" action="<?php echo base_url();?    >index.php/users/save_record">
    <div class="form-group">
      <label for="usr">Name:</label>
      <input type="text" class="form-control" name="name" id="name">
    </div>
    <div class="form-group">
      <label for="pwd">Age:</label>
      <input type="text" class="form-control" name="age" id="age">
    </div>
    <div class="form-group">
      <label for="pwd">Sex:</label>
      <input type="text" class="form-control" name="sex" id="sex">
    </div>
    <div class="form-group">
      <label for="pwd">Phone Number:</label>
      <input type="text" class="form-control" name="phno" id="phno">
    </div>
    <div class="form-group">
      <input class="btn btn-primary" type="submit" name="submit" value="Send" />
    </div>
</form>

控制器

public function save_record()
    {   
        if ($this->input->post('submit')==true) 
        {
        $udata['name']=$this->input->post['name'];
        $udata['age']=$this->input->post['age'];
        $udata['sex']=$this->input->post['sex'];
        $udata['phno']=$this->input->post['phno'];
        //$this->Users_model->save_user($udata);    
        var_dump($udata);
        }   
    }

结果

array(4) { ["name"]=> NULL ["age"]=> NULL ["sex"]=> NULL ["phno"]=> NULL }

【问题讨论】:

  • 创建这样的数组 $data = array("name"=>$this->input->post('age'),"name"=>$this->input->post( '年龄'));
  • @Yaseen 上面示例中使用的推送到数组的方法很好。 $this->input->post['phno'] 的用法不对,应该是 $this->input->post('phno');
  • 非常感谢 ....您编写的代码运行良好

标签: php forms codeigniter controller


【解决方案1】:

您的语法错误。试试这个。

if ($this->input->post('submit') == true) {
    $udata['name'] = $this->input->post('name');
    $udata['age'] = $this->input->post('age');
    $udata['sex'] = $this->input->post('sex');
    $udata['phno'] = $this->input->post('phno');
    //$this->Users_model->save_user($udata);    
    var_dump($udata);
}

【讨论】:

    猜你喜欢
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多