[英]CodeIgniter: How to get input from View and use it in the controller
I'm quite new to CodeIgniter (using PHP). 我是CodeIgniter的新手(使用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. 我输入一个名称(我将使用变量$ username)并将其保存在我拥有的数据库中。 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? 是否以某种方式将$ username传递给Controller中的函数,然后将该值传递给我的Model? Thank you very much, a simple example would be much appreciated!! 非常感谢您,一个简单的例子将不胜感激!
<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 在User_model中
function Add($arrData) {
if($this->db->insert(tableName, $arrData))
return true;
else
return false;
}
A quick Google search brings up the documentation . 快速的Google搜索会显示文档 。 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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.