[英]Ajax displaying errors in login form without reloading page
I have a login form which currently reloads to a new page whenever there is an error ie 'email address not registered'.我有一个登录表单,当出现“电子邮件地址未注册”错误时,该表单当前会重新加载到新页面。 I need the errors to display on the same page wihtout reloading to a new page.
我需要将错误显示在同一页面上,而无需重新加载到新页面。 I followed a tutorial on how to do it with Ajax, but it doesn't seem to work, the page still reloads.
我遵循了有关如何使用 Ajax 执行此操作的教程,但它似乎不起作用,页面仍在重新加载。 The ajax code is:
ajax代码如下:
$("#login_button").click(function(){
$.post($("#login_form").attr("action"), $("#login_form :input").serializeArray(), function(info){$("#login_errors").html(info);});
clearInput();
});
$("login_form").submit(function(){
return false;
});
function clearInput(){
$("#login_form: input").each(function() {
$(this).val('');
});
}
My code for my login form is:我的登录表单代码是:
<form id= "login_form" action="login.php" method="post">
<span id="login_errors"></span>
<label>Email Address</label>
<input type="text" name="email" id="email" required="required"/>
<br />
<label>Password</label>
<input type="password" name="password" id="password" required="required"/>
<br />
<div class="checkbox">
<input id="remember" type="checkbox" />
<label for="remember">Keep me signed in</label>
</div>
<div class="action_btns">
<div class="one_half last"><input type="submit" class="btn btn-blue" id="login_button" value="Login"></div>
<div class="one_half last"><a href="#" id="register_form"
class="btn">Sign up</a></div>
</div>
</form>
And I linked the ajax script with:我将 ajax 脚本与:
<script type="text/javascript" src="login_Ajax.js"></script>
You should prevent the default action (submit) from being fired by your submit button.您应该防止提交按钮触发默认操作(提交)。
Update your code like so:像这样更新你的代码:
$("#login_button").click(function(){
$.post($("#login_form").attr("action"), $("#login_form :input").serializeArray(), function(info){$("#login_errors").html(info);});
clearInput();
// Prevent the default action from occurring.
return false;
});
The first thing that you have to do when you starting to use AJAX, is know exactly how show information through elemnts like classes into a div or any tag of HTML5.当您开始使用 AJAX 时,您必须做的第一件事就是确切地知道如何通过类等元素将信息显示到 div 或 HTML5 的任何标签中。 Please see the next code because I will comment it for you.
请查看下一个代码,因为我会为您注释。
//This is our DOM on JQUERY or JAVASCRIPT named as main.js
$(function() {
//First we have to listen on a event click (button) and extract the values of the inputs
$('body').on('click','.myButtonClass', function() {
$email = $('#emailID').val();
$password = $('#passwordID').val();
//We save in a object our values
$x = {
action:'register',
email:$email,
password:$password
};
//We execute ajax calling a php file for POST method
$.post('file.php',$x,function(callback) {
$('.answer').html(callback);
});
});
});
/*END OF DOM AND MAIN.JS*/
//BEGIN OUR PHP FILE WHERE WE'LL RECIEVE ALL THE DATA FROM THE AJAX FILE
<?php
//we save the action sended trough ajax
$action = $_POST['action'];
//we do a switch for determinate what value have the action variable
switch ( $action ) :
case 'register':
//we save with addslashes for security in the data base
$email = addslashes($_POST['email']);
$password = addslashes($_POST['password']);
//If your objetive is validate if the email and password were send in the form, you can do something like this
$validate = strlen($email)*strlen($password);
//This say that if email * password are > 0, the data were send. Otherwise, this operation gonna be equal zero
//and thats means that both or one of the data were not recieve in this file trough ajax.
if ( $validate > 0 ) :
echo 'You login!';
else :
echo 'You are not write all the inputs.';
endif;
break;
endswitch;
?>
/*END OF PHP FILE*/
//BEGIN OUR FORM IN HTML
<form>
<input type="email" id="emailID" placeholder="Write here your email">
<input type="password" id="passwordID" placeholder="Write here your password">
<input type="button" class="myButtonClass">
</form>
<!--This will be where we display the answer with ajax, in the element who have the class answer-->
<div class="answer"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.