[英]AJAX validation script doesn't execute before login PHP script
I'm trying to use AJAX to validate my form, but it isn't working and I have no idea why. 我正在尝试使用AJAX验证我的表单,但是它不起作用,我也不知道为什么。 It is still executing login_action.php instead of the AJAX validation.
它仍在执行login_action.php而不是AJAX验证。 I've created a modal login form and want the error message to be displayed inside that popup.
我已经创建了一个模式登录表单,并希望该错误消息显示在该弹出窗口内。 Now it is still redirecting to the login_action.php file first.
现在,它仍然仍然首先重定向到login_action.php文件。
Script: 脚本:
<script>
$('document').ready(function(event)
{
event.preventDefault();
/* validation */
$("#loginform").validate({
rules:
{
password: {
required: true,
},
email: {
required: true,
email: true
},
},
messages:
{
password:{
required: "please enter your password"
},
email: "please enter your email address",
},
submitHandler: submitForm
});
/* validation */
/* login submit */
function submitForm()
{
var data = $("#loginform").serialize();
$.ajax({
type : 'POST',
url : 'login_action.php',
data : data,
beforeSend: function()
{
$("#error").fadeOut();
$("#submitter").html('<span class="glyphicon glyphicon-transfer"></span> sending ...');
},
success : function(response)
{
if(response=="ok"){
$("#submitter").html('<img src="btn-ajax-loader.gif" /> Signing In ...');
setTimeout(' window.location.href = "index.php"; ',4000);
}
else{
$("#error").fadeIn(1000, function(){
$("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> '+response+' !</div>');
$("#btn-login").html('<span class="glyphicon glyphicon-log-in"></span> Sign In');
});
}
}
});
return false;
}
/* login submit */
});
</script>
Login popup: 登录弹出窗口:
<div style="color:black;" id="loginpop" class="modal">
<form class="loginform animate" action="login_action.php" id="loginform" method="post" onsubmit="return do_login();">
<div class="maincontainer">
<span onclick="document.getElementById('loginpop').style.display='none'" class="close" title="Close Modal">×</span>
</div>
<div class="undermaincontainer">
<div id="error">
<!-- error will be shown here ! -->
</div>
<label><b>E-mailadres</b></label>
<input type="email" placeholder="E-mailadres" name="email" id="emailid" >
<label><b>Wachtwoord</b></label>
<input type="password" placeholder="Wachtwoord" name="password" id="passwordid" >
<input type= "submit" class="loginbtn" value="Inloggen" name="submitter" id="submitter">
<label class="signuplink"><a href="registreren.php">Klik hier om een account aan te maken</a></label>
</div>
</form>
</div>
<script>
var modal = document.getElementById('loginpop');
// close popup als de gebruiker naast de popup klikt
window.onclick = function(event)
{
if (event.target == modal)
{
modal.style.display = "none";
}
}
</script>
PHP login_action PHP login_action
<?php
session_start();
include('sslsecurity.php');
include('file:///var/www/configure.php');
if(isset($_POST['submitter'])) {
$email = $_POST['email'];
$password = $_POST['password'];
// de gebruiker moet natuurlijk wel iets invullen
if(!empty($_POST['email'])) {
// de query in SQL
$query = $conn->prepare("SELECT role, email, pass FROM WebsiteUsers WHERE email = ? AND pass = SHA1(?) ");
$query->bindValue('1', $email);
$query->bindValue('2', $password);
$query->execute();
// user id van de user achterhalen
// resultaat met waarden
$count = $query->rowCount();
if($count==1)
{
while($row=$query->fetch(PDO::FETCH_ASSOC))
{
if($row['role'] == 'admin')
{
$_SESSION['loginright'] = $row['role'];
}
else
{
$_SESSION['loginright'] = $row['role'];
}
}
// session variable om de user te herkennen aanmaken
$_SESSION['email'] = $email;
// terug naar de index pagina
echo "ok";
}
else
{
echo "fail";
}
}
}
?>
<?php
$conn = null;
?>
I've been searching on the internet a while but couldn't find an answer that worked for me. 我已经在互联网上搜索了一段时间,但找不到适合我的答案。 I tried event.preventDefault();, but that doesn't work as well.
我尝试了event.preventDefault();,但效果不佳。
Thank you 谢谢
It doesn't appear that you actually have an even listener on your submit button: 看来您的提交按钮上实际上并没有一个监听器:
$("#submit").click(function(event){
//execute validation and ajax here.
event.preventDefault();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.