我有一个登录表单,当出现错误,即“未注册的电子邮件地址”时,该表单当前会重新加载到新页面。我需要将错误显示在同一页面上,而不是重新加载到新页面。我按照一个关于如何使用Ajax的教程,但似乎没有用,页面仍然重新加载。 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('');
});
}
我的登录表单的代码是:
<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>
我将ajax脚本与:
联系起来<script type="text/javascript" src="login_Ajax.js"></script>
答案 0 :(得分:1)
您应该阻止提交按钮触发默认操作(提交)。
更新你的代码:
SELECT * FROM car_space INNER JOIN transaction ON car_space.carSpaceId = transaction.carSpaceId ORDER BY transactionId;
UNION
SELECT * FROM sport_facilities INNER JOIN transaction ON sport_facilities.sportFacilitiesId = transaction.sportFacilitiesId ORDER BY transactionId;
答案 1 :(得分:0)
当你开始使用AJAX时,你必须要做的第一件事就是确切地知道如何通过elemnts将信息显示为div或HTML5的任何标记。请参阅下一个代码,因为我会为您发表评论。
//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>