CodeIgniter:如何从View获取输入并在控制器中使用它 [英] CodeIgniter: How to get input from View and use it in the controller

查看:158
本文介绍了CodeIgniter:如何从View获取输入并在控制器中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

在控制器中

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

在User_model中

in User_model

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

这篇关于CodeIgniter:如何从View获取输入并在控制器中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆