我目前正在使用Codeigniter。我正在尝试创建一个注册页面。 注册在视图文件中。在填写注册表格后,我想将用户的信息发送给控制器。并且在我希望它检查信息是否有效之后(用户名尚未使用..有效的电子邮件地址等)。如果它是真的,则将相同的信息发送到添加到数据库的Model。如果它是假的,所以加载视图注册页面。我发现很多话题都是关于如何从控制器发送变量来查看(在CodeIgniter网站上向View添加动态数据)或控制器来进行建模,但没有发现任何有关view-> controller的有用信息。这里是我想做的事: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请求控制器/法从视图 –
看到完整的工作代码验证,查询,保存数据,如果表单验证失败,在我的个人资料链接,也有一个工作演示' - >' – Viral
@ S7_0你检查下面的答案 –