我在结合 CodeIgniter 和 AJAX 时遇到了麻烦。我有一个注册表单。我想要的是,当用户单击我主页上的注册按钮时,他会在弹出窗口中获得一个注册表单。如果他在表单中输入了错误的详细信息,他会在弹出窗口中提示您的详细信息无效,他不能被重定向到新页面以显示错误。所以我使用 bpopup 使我的表单弹出和 ajax 请求,但我无法执行 form_validation,我有点困惑。如果用户未通过表单验证,则会因为视图正在加载而向用户提醒 HTML 文档。如果我不加载视图,它将不会显示登录页面。如果我直接加载视图,我将如何显示用户个人错误,例如密码必须是 6 个字符长。这是我的代码:
我的登录视图:
<html>
<head>
<title>Sign In</title>
<script type="text/javascript">
var frm = $('form');
frm.submit(function () {
$("#status").empty();
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
if (data == "invalid") {
$('#status').html("Invalid Login Details");
}
else if(data == "valid")
{
$('#status').html("Login successful");
setTimeout(function(){
location.reload();
},600);
}
else
{
$('#status').html("Your Account is not Activated");
}
}
});
return false;
});
</script>
</head>
<body>
<div class="Header">
<h3>Sign In </h3>
</div>
<div id="status"></div>
<div class="main">
<?php echo form_open('xyz/signin'); //Renders the form element and adds extra functionality like Adds a hidden CSFR prevention field.?>
<h5>Email</h5> // For the time being I have remove echo form_error.
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="25"/>
<h5>Password *</h5> // For the time being I have remove echo form_error.
<input type="password" name="password" size="25"/>
<div><input type="submit" value="Submit" "/></div>
</form>
</div>
</body>
</html>
这是我 xyz 控制器的登录功能:
public function signin()
{
$this->load->helper(array('form','url'));
$this->load->library('form_validation');
$config = array(
array(
'field' => 'email',
'label' => 'Email Address',
'rules' => 'trim|required|valid_email|xss_clean'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|xss_clean'
)
);
$this->form_validation->set_rules($config);
if($this->form_validation->run() == FALSE) /
{
$this->load->view('pinflag/login.php');
}
else //format of form is correct
{
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$data = array(
'email' => $email,
'password' => $password
);
$result = $this->user_model->get_users($data);
if($result->num_rows == 1) //login details are correct
{
$result = $result->result_array();
if($result[0]['status'] == 0)
{
echo "notActivated";
}
else
{
$this->session->set_userdata('id',$result[0]['id']);
$this->session->set_userdata('name',$result[0]['fname']);
echo "valid";
}
}
else //when user enter invalid login details
{
echo "invalid";
}
}
}
抱歉,当我在这里粘贴代码时,缩进搞砸了。