CodeIgniter: How to get input from View and use it in the controller

谁说胖子不能爱 提交于 2019-12-05 04:04:24

问题


I'm quite new to CodeIgniter (using PHP). So right now I have a textbox on my webpage where visitors are requested to enter in their names. I take in a name (i'll use the variable $username for it) and I want to save it in a database I have. I am wondering how I can do that. Do I somehow pass $username to a function in the Controller and then pass that value to my Model? Thank you very much, a simple example would be much appreciated!!


回答1:


<form method="post" action="<?php echo base_url();?>controller/save" name="form">
  <label>User Name</label>
  <input type="text" name="Username">   
  <input type="submit" value="Add"/>
</form>

in controller

function save
    {
    $arrData["Username"] = $this->input->post("Username");
    $this->User_model->Add($arrData); 
    } 

in User_model

function Add($arrData) { 
        if($this->db->insert(tableName, $arrData))
          return true;
        else
          return false;
    }



回答2:


A quick Google search brings up the documentation. You should take a look at it.

The example they give:

$data = array(
   'title' => 'My title' ,
   'name' => 'My Name' ,
   'date' => 'My date'
);

$this->db->insert('mytable', $data);
// produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')


来源:https://stackoverflow.com/questions/14764458/codeigniter-how-to-get-input-from-view-and-use-it-in-the-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!