我知道有很多类似的问题,但我一直无法找到一个解决我的问题。
事情是我想检查用户/密码在我的表单是否存在对mysql数据库,我这样做是通过调用javascript函数在表单的onsubmit属性,通过AJAX将输入发送到php检查它,并将结果返回到javascript函数,提醒用户并返回false或返回true,以便表单提交。
HTML<span id="error_message" style="color:#CC3300;font-size:15px" ></span><br/>
<form method="post" action="./includes/process_login.php" onsubmit="return validateForm()" name="login_form">
<div>
<label for="email">Email</label><br/>
<input placeholder="Introduce tu email" type="text" id="email" name="email" maxlength="50" />
</div>
<div>
<label for="password">Password</label><br/>
<input placeholder="Introduce tu password" type="password" name="password" id="password" maxlength="16" />
</div>
<div>
<a class="passwordReset" href="./admin/passwordRecovery.php">He olvidado mi contraseña...</a>
</div>
<input type="submit" id="submit_button" value="ENTRAR" />
</form>
JAVASCRIPT/AJAX
function validateForm() {
var pattern = new RegExp(/* Email validation pattern */);
var isValid;
if (pattern.test($("#email").val())) {
$.ajax({
url: "../includes/process_login.php",
cache: false,
type: "POST",
data: "email=" + $("#email").val() + "&password=" + $("#password").val(),
success: function(data){
if (data == '1') {
isValid = true;
} else {
$("#error_message").text('Email/Password incorrectos!CCC');
isValid = false;
}
},
error: function(){
$("#error_message").text('Email/Password incorrectos!');
isValid = false;
}
});
} else {
$("#error_message").text('El email debe tener un formato válido');
isValid = false;
}
return isValid;
}
PHP验证和表单操作:
<?php
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start();
if (isset($_POST['email'], $_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
if (login($email, $password, $mysqli) == true) {
// Login success
header("Location: ../graficas.php");
echo '1';
} else {
// Login failed
echo '0';
}
} else {
// The correct POST variables were not sent to this page.
echo '0';
}
exit;
表单总是在email输入强制email正则表达式时提交,所以问题一定是在ajax函数内部。但我不知道那是什么,几个小时后,我比以往任何时候都更接近精神病院。
提前感谢!
在你的validateForm()
中添加这个来防止表单提交
e.preventDefault();
这样的function validateForm(e){
var pattern = new RegExp(/* Email validation pattern */);
var isValid;
if (pattern.test($("#email").val())) {
e.preventDefault();
//Do rest of your code
我终于可以通过将表单和所有逻辑放在一个PHP文件中来完成它:
<!-- The HTML login form -->
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<div>
<label for="username">Login</label><br/>
<input placeholder="Introduce tu login" type="text" id="user" name="username" maxlength="50" />
</div>
<div>
<label for="password">Password</label><br/>
<input placeholder="Introduce tu password" type="password" name="password" id="password" maxlength="16" />
</div>
<div><a class="passwordReset" href="./admin/passwordRecovery.php">He olvidado mi contraseña...</a></div>
<input type="submit" id="submit_button" name="submit" value="ENTRAR" />
</form>
<?php
if (isset($_POST['submit'])){
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if (login($username, $password, $mysqli) == true) {
// Login success
echo '<script type="text/javascript">window.location.replace("graficas.php");</script>';
} else {
// Login failed
echo '<script type="text/javascript">document.getElementById('error_message').innerHTML = "Login/Password incorrectos!";</script>';
}
}
?>
当需要修改元素并避免AJAX问题时,通过'echo'生成JS脚本。
关键元素是
isset($_POST['submit'])
来检查表单之前是否已提交。
我从这个不错的教程中获得了这个想法:http://w3epic.com/php-mysql-login-system-a-super-simple-tutorial/