我目前正在使用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脚本是
<?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";
}
?>
现在,如果我们收到一条消息&#34;您已成功登录&#34;刷新当前页面,否则只显示错误消息,不刷新页面。
我可以选择添加
<meta http-equiv="refresh" content="30">
但这只适用于chrome而不是firefox。
答案 0 :(得分:1)
试试这个:
<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;
if (result == "You are bot a valid user") {
window.location.href = "mypage.html"
}
}
});
}
return false;
});
});
</script>
答案 1 :(得分:1)
您可以将条件置于循环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>
答案 2 :(得分:0)
<script>
$(document).ready(function(){
$("#submit").click(function(){
var emailnew = $("#email").val();
var password = $("#password").val();
var dataString = '&email='+ email + '&password='+ password;
if(emailnew == null || password == null)
{
document.getElementById('error3').innerHTML="Please Fill All Fields";
}
else
{
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
cache: false,
success: function(result)
{
if(result == "logged in true")
{
window.setTimeout(function(){location.reload()},1000); //reload with time
location.reload(); //reload without time
}
else
{
document.getElementById('error3').innerHTML=result;
}
}
});
}
return false;
});
});
</script>
答案 3 :(得分:0)
你的process.php应该返回一个像successl(true)和notccessful(false)标志的结果。或者userID,例如,如果它是成功的,还有一些其他标志,如果它不是。你不应该在proccess.php文件中回应通知。然后你可以在你的javascript代码中有这样的东西:
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
cache: false,
success: function(result){
if(result=='successful'){
showMessageAndredirectFunction();
//in this functionyou can show the message in your html page like the way you do for error and with a delay redirect the page with javascript
else
document.getElementById('error3').innerHTML="Bad usernaem or password";
}});
如果成功,您可以在process.php中设置会话。
答案 4 :(得分:0)
window.location.reload()
用于通过javascript重新加载页面。
或者您可以使用window.location.href=window.location.href;