这是我的控制器功能
public function verifyUser()
{
$userName = $_POST['email'];
$userPassword = $_POST['password'];
$var=array('email'=>$userName,'password'=>$userPassword);
$check=$this->mymodel->login_validation($var);
//$status = array("STATUS"=>"false");
if(count($check))
{
redirect('main/valid_login');
}
else
{
echo "<div style='border:1px solid red;font-size: 11px;margin:0 auto !important;'>Could't Authorize to the system! Try again with valid credentials.</div>" ;
}
}
这是我的ajax功能
<script>
function makeAjaxCall(){
$.ajax({
type: "post",
url: "<?php echo site_url('main/verifyUser');?>",
cache: false,
data: $('#userForm').serialize(),
success:function(msg)
{
$('#show_id').html(msg);
}
});
}
</script>
这是我的表格
<form name="userForm" id="userForm" action="">
<div id="show_id"></div>
<fieldset>
<p><label for="email">E-mail address</label></p>
<p><input type="email" id="email" placeholder="enter your email id" name="email"></p> <!-- JS because of IE support; better: placeholder="mail@address.com" -->
<p><label for="password">Password</label></p>
<p><input type="password" id="password" placeholder="*******" name="password" style="width: 328px;"></p> <!-- JS because of IE support; better: placeholder="password" -->
<p><input type="button" value="Sign In" onclick="javascript:makeAjaxCall();"></p>
</fieldset>
</form>
一切正常,但是当我输入有效的用户名和密码时,它没有重定向到任何页面,所以请帮我这个
答案 0 :(得分:1)
将JSON数据作为响应发送给ajax并根据需要进行处理 Contorller:
public function verifyUser() {
$userName = $_POST['email'];
$userPassword = $_POST['password'];
$var=array('email'=>$userName,'password'=>$userPassword);
$check=$this->mymodel->login_validation($var);
//$status = array("STATUS"=>"false");
if(count($check)) {
$this->output
->set_content_type("application/json")
->set_output(json_encode(array('status'=>true, 'redirect'=>base_url('main/valid_login') )));
}
else {
$this->output
->set_content_type("application/json")
->set_output(json_encode(array('status'=>false, 'error'=>'Could't Authorize to the system! Try again with valid credentials.')));
}
}
使用ajax处理JSON数据。
$.ajax({
type: "post",
url: "<?php echo site_url('main/verifyUser');?>",
cache: false,
data: $('#userForm').serialize(),
dataType: 'json',
success:function(response) {
if( response.status === true )
document.location.href = response.redirect;
else
$('#show_id').html("<div style='border:1px solid red;font-size: 11px;margin:0 auto !important;'>"+response.error+"</div>");
}
});
答案 1 :(得分:0)
控制器功能:
public function verifyUser()
{
$userName = $_POST['email'];
$userPassword = $_POST['password'];
$var=array('email'=>$userName,'password'=>$userPassword);
$check=$this->mymodel->login_validation($var);
//$status = array("STATUS"=>"false");
if(count($check))
{
echo 1;
}
else
{
echo "<div style='border:1px solid red;font-size: 11px;margin:0 auto !important;'>Could't Authorize to the system! Try again with valid credentials.</div>" ;
}
}
Ajax功能
<script>
function makeAjaxCall(){
$.ajax({
type: "post",
url: "<?php echo site_url('main/verifyUser');?>",
cache: false,
data: $('#userForm').serialize(),
success:function(msg)
{
if(msg== "1")
{
//redirect here
}
$('#show_id').html(msg);
}
});
}
</script>