您好我是codeigniter框架的初学者,我遇到了一些问题。
我做了一个注册表单,按下 REGISTER 按钮后,我想调用一个js函数formValidation()
来检查一些基本的东西(比如每个字段是否填满) )。
如果一切正常,那么我想将这些值从注册表单提交到控制器的方法checkExistance()
,其任务是从users_model checkDoesUserExists($username)
调用该方法,该方法将检查我们是否有在我们的数据库中具有该用户名的用户,他将返回true或false,具体取决于base的结果。
我的问题是如何从我的表单提交数据并调用控制器方法并将数据传递给该方法(如$ username ...),如果我错过了代码中的其他内容?
我真的不知道如何从我的js调用控制器方法? 感谢大家愿意帮助我!
这是我的html页面:registration.php
<html>
<head>
<script>
function formValidation(){
var username = document.registrationForm.username.value;
var email = document.registrationForm.email.value;
var name = document.registrationForm.name.value;
var surname = document.registrationForm.surname.value;
if(username == ""){
alert('enter your username');
document.registrationForm.username.focus();
}else if(email == ""){
alert('enter your email');
document.registrationForm.email.focus();
}else if(name == ""){
alert('enter your name');
document.registrationForm.name.focus();
}else if(surname == ""){
alert('enter your surname');
document.registrationForm.surname.focus();
}else{
//alert('OK');
document.registrationForm.submit();
//I would like to call controller's method from here and to pass data to that method - "checkExistance"
}
}
</script>
</head>
<body>
<fieldset>
<legend>Registration form</legend>
<form name="registrationForm" method="post" aciton="<?php echo $_SERVER['PHP_SELF'];?>">
<table>
<tr><td>Username</td><td> <input type="text" name="username" placeholder="insert your username"> </td></tr>
<tr><td>Email</td><td> <input type="text" name="email" placeholder="insert your email"> </td></tr>
<tr><td>Name</td><td> <input type="text" name="name" placeholder="insert your name"> </td></tr>
<tr><td>Surname</td><td> <input type="text" name="surname" placeholder="insert your surname"> </td></tr>
<tr><td> <input type="button" value="REGISTER" onclick="formValidation()"> </td><td> <input type="reset" value="Clear form"> </td></tr>
</table>
</form>
</fieldset>
</body>
</html>
这是我的控制器:users_controler.php
<?php
class Users_controler extends CI_Controller{
function index(){
$this->load->view('registration');
}
function checkExistance(){
$username['username'] = $this->input->post('username');//// ???? This is probably wrong
$this->load->model('users_model');
$result = $this->users_model->checkDoesUserExists($username);
if($result == true){
echo 'OK - username is available, we will now activate in the users_model "addUser()" method to add a user in database';
} else {
echo 'ALERT - User already exists';
}
}
}
这是我的模型:users_model.php
<?php
class User_model extends CI_Model{
function checkDoesUserExists($username){
$query = $this->db->query("select * from users where username = '$username'");
if($query->num_rows()>0){
return false;
}
return true;
}
function addUser(){
// Adding the user in this step
}
}
感谢大家愿意帮助我!!!!
答案 0 :(得分:0)
在控制器动作函数中使用表单验证,如下所示。
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
这里你需要关注回调函数username_check,类似于你需要在checkExistance函数中做的改变,休息你可以在这里学习。
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html