我知道有很多类似的问题,但我找不到能解决问题的问题。
问题是我想检查我的表单中的用户/密码是否存在于mysql数据库中,我是通过调用表单的onsubmit属性中的javascript函数来实现的,该函数通过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;
当电子邮件输入符合电子邮件正则表达式时,表单始终提交,因此问题必须在ajax函数内。但我无法弄清楚它是什么,几个小时后,我比以往任何时候都更接近庇护。
提前致谢!
答案 0 :(得分:0)
在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
答案 1 :(得分:0)
我终于可以通过将表单和所有逻辑放在一个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>';
}
}
?>
通过回声&#39;生成JS脚本当需要修改元素并避免AJAX问题时。
这里的关键元素是
isset($_POST['submit'])
检查表单是否先前已提交过。
我从这个很好的教程中提出了这个想法:http://w3epic.com/php-mysql-login-system-a-super-simple-tutorial/