使用ajax成功提交表单后重新加载页面
问题描述:
我目前正在使用ajax进行登录
I am currently a making a log in form using ajax
<div id='error3'></div>
<input type='email' required name='email' id='email' placeholder='Email'>
<input type='password' required name='password' id='password' placeholder='password'>
<input type='submit' name='submit99' id='submit' value='login'>
我的Java脚本是
<script>
$(document).ready(function(){
$("#submit").click(function(){
var emailnew = $("#email").val();
var password = $("#password").val();
var dataString = '&email='+ email + '&password='+ password;
if(emailnew==''|| password='')
{
document.getElementById('error3').innerHTML="Please Fill All Fields";
}
else
{
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
cache: false,
success: function(result){
document.getElementById('error3').innerHTML=result;
}
});
}
return false;
});
});
我的process.php脚本是
and my process.php script is
<?php
if(isset($_POST['email']))
{
if (filter_var($email99, FILTER_VALIDATE_EMAIL))
{
$con=mysqli_connect('localhost','root','password','database');
$query="SELECT * FROM TABLENAME WHERE USER='' AND PASSWORD=''";
$result=mysqli_query($con,$query);
if(mysqli_num_rows($result)!=0)
{
echo "You are successfully logged in";
login user
start a cookie or session
}
else
{
echo "You are bot a valid user";
}
}
else
echo "not a valid email";
}
?>
现在,如果我们收到一条消息您已成功登录",则刷新当前页面,否则仅显示错误消息,而不刷新页面.
now if we recive a message "You are successfully logged in" refresh the current page otherwise show only error message and do not refresh the page.
我可以选择添加
<meta http-equiv="refresh" content="30">
但这只能在chrome中使用,而不能在Firefox中使用.
but this works only in chrome not in firefox.
答
您可以将条件放入循环 success:function(result){
You can put the condtion inside loop success: function(result){
<script>
$(document).ready(function () {
$("#submit").click(function () {
var emailnew = $("#email").val();
var password = $("#password").val();
var dataString = '&email=' + email + '&password=' + password;
if (emailnew == '' || password = '') {
document.getElementById('error3').innerHTML = "Please Fill All Fields";
} else {
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
cache: false,
success: function (result) {
if (result == "You are successfully logged in") {
document.location.href = 'login.htm';
} else {
document.getElementById('error3').innerHTML = result;
}
}
});
}
return false;
});
});
</script>