本文介绍了jQuery中的页面重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
成功登录后,我想重定向页面.但是此代码中的问题是,如果我输入错误,它也会转到主页.所以我删除了这个语句"window.location = home.php".我现在该怎么办?有人帮我吗?
I want to redirect the page after login succesfully completed. but the problem in this code is, if i give wrong input, it also go to home page. so i removed this statement 'window.location = home.php'. what could i do now? does anyone help me guys?
<script>
$(document).ready(function(){
$('#reg_form').parsley();
$('#reg_form').on('submit',function(event){
event.preventDefault();
if($('#reg_form').parsley().isValid())
{
$.ajax({
url:"db.php",
method:"POST",
data:$(this).serialize(),
beforeSend:function(){
$('#submit').attr('disabled','disabled');
$('#submit').val('submitting the value');
},
success:function(data){
$('#reg_form')[0].reset();
$('#reg_form').parsley().reset();
$('#submit').attr('disabled',false);
$('#submit').val('submit');
$('#message').html(data);
}
});
}
});
});
</script>
db.php
<?php
//user login
if(isset($_POST['email']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$query = "SELECT * FROM tbl_register where email ='$email' AND password = '$password'";
$result = mysqli_query($connect,$query);
if(mysqli_num_rows($result)>0)
{
echo "login successfully completed";
}
else
{
echo "login failed";
}
}
?>
推荐答案
最好从服务器端传递是否成功登录的标志值.
Its better to pass flag value that login successfully or not from server side.
赞:
如果您要编写以下代码:
If you will write below code:
if(mysqli_num_rows($result)>0)
{
echo 1;
}
else
{
echo 0;
}
然后在ajax调用中,您可以检查数据
And then in ajax call you can check data
$.ajax({
url:"db.php",
method:"POST",
data:$(this).serialize(),
beforeSend:function(){
$('#submit').attr('disabled','disabled');
$('#submit').val('submitting the value');
},
success:function(data){
if(data==1){
window.location = home.php
}
$('#reg_form')[0].reset();
$('#reg_form').parsley().reset();
$('#submit').attr('disabled',false);
$('#submit').val('submit');
$('#message').html(data);
}
});
希望我的回答对您有所帮助.
I hope my answer helps you.
这篇关于jQuery中的页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!