【发布时间】:2015-09-17 08:52:42
【问题描述】:
我目前正在使用 Codeigniter。我正在尝试创建一个注册页面。 注册在视图文件中。填写注册表格后,我想将用户信息发送给控制器。在我希望它检查信息是否有效之后(用户名尚未使用..有效的电子邮件地址等等)。如果是真的,那么将相同的信息发送到添加到数据库中的模型。如果它是假的,那么加载查看注册页面。我发现了很多关于如何将变量从控制器发送到视图(将动态数据添加到 CodeIgniter 网站上的视图)或控制器到模型的话题,但没有发现关于视图->控制器的任何帮助。这是我想做的:
VIEW CONTROLER
_______________ ______________________
|REGISTRATION:| -----------> |Check valid argument|
--------------- ----------------------
/|\ | |
|___________________| If it's valid information
(if no valid) |
|
\ /
MODEL
____________________________
|Add information to database|
-----------------------------
这是我的表格:
<h1>create a new account</h1>
<?php echo validation_errors();?>
<?php echo form_open('index.php/add_db');?>
<label for='name'>Name</label>
<input type='text' size='20' id='name' name='name'/>
<br />
<label for='last_name'>Last_Name</label>
<input type='text' size='20' id='last_name' name='last_name'/>
<br />
<label for='username'>Username</label>
<input type='text' size='20' id='username' name='username'/>
<br />
<label for='password'>Password</label>
<input type='password' size='20' id='username' name='password'/>
<br />
<label for='confirmation_password'>Password confirmation</label>
<input type='password' size='20' id='password' name='password'/>
<br />
<input type="submit"/>
</form>
这是我的控制器文件
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Add_db extends CI_Controller
{
function __construct()
{
parent:: __construct();
$this->load->database();
}
function index()
{
//check if the password does match
//check before if the username is not already user
//open the model function which add to the database :)
}
}
不知道在模型中查看用户信息是否更好。因为我知道模型处理数据库和查询。 否则控制器处理计算部分,因此控制器可能更适合此任务。 但没关系。是否可以发送 view---->controller 之类的信息? 还是我必须找到另一种检查信息的方式? 像控制器 -> 视图 -> 模型?
谢谢
【问题讨论】:
-
从视图向控制器/方法发出ajax请求
-
查看完整的工作代码,包括验证、查询、如果表单验证失败则保留数据,在我的个人资料中的链接中,还有一个工作演示
--> -
@S7_0你有没有检查下面的答案
标签: php html codeigniter