我在同一页面上有一个登录和注册表单,我用jQuery隐藏,因此一次只有一个表单处于活动状态。当我注册表单处于活动状态时,请使用与我的PHP if语句不匹配的电子邮件填写电子邮件字段,然后按提交,然后页面刷新并激活登录表单。
我希望页面执行的是运行PHP脚本,当PHP if语句失败时,它将阻止刷新页面,然后打印位于PHP else语句中的消息。
代码如下所示:
PHP:
if(isset($_POST['register'])) {
$email = strip_tags($_POST['email']);
$password = strip_tags($_POST['password']);
$request = $_POST['request'];
$email = $DBcon->real_escape_string($email);
$password = $DBcon->real_escape_string($password);
$email = $_POST['email'];
$password = $_POST['password'];
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$check_email = $DBcon->query("SELECT email FROM members WHERE email='$email'");
$count = $check_email->num_rows;
if ($count==0) {
if (preg_match('/^[a-z]{6}+[0-9]{2}+@student.westerdals.no+$/i', $email)) {
$query = "INSERT INTO members(email, password) VALUES('$email', '$hashed_password')";
$result = $DBcon->query($query);
header("Location: home.php");
die();
} else {
echo "You must provide a valid student email!";
}
} else{
echo "Email already taken!";
}
}
$DBcon->close();
jQuery:
$(document).ready(function () {
var request = $(this).serialize();
$("#registerform").on('submit', function (event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'registration.php',
data: request,
success: function(){
$(this).submit();
console.log("test");
},
error: function () {
event.preventDefault();
alert("fail");
}
});
});
});
答案 0 :(得分:0)
我假设您的代码正常运行,并且您通过javascript中的帖子正确接收数据,那么只需要完成这些更改。
py -2.7 -m pip install mypackage