CodeIgniter:如何从View获取输入并在控制器中使用它

CodeIgniter:如何从View获取输入并在控制器中使用它

问题描述:

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!!

我是CodeIgniter的新手(使用PHP)。 所以现在我的网页上有一个文本框,要求访问者输入他们的名字。 我接受一个名字(我将使用变量$ username),我想将它保存在我拥有的数据库中。 我想知道我该怎么做。 我是否以某种方式将$ username传递给Controller中的函数,然后将该值传递给我的模型? 非常感谢,一个简单的例子将非常感谢!! p> div>

<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;
    }

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