我正在尝试创建一个密码重置表单,当用户单击其电子邮件中的链接时,会将其发送到密码重置页面。
密码表单已通过FormValidation验证,但我遇到的问题是,当用户提交时,页面会刷新,清除GET变量并且不会进入if($_SERVER['REQUEST_METHOD'] == 'POST')
部分。
您可以看到上传的代码here。
我尝试了各种解决方案,但不知道什么是错的。一切都有帮助!
谢谢你们!
这是我的PHP代码:
// ERROR REPORTING
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// END ERROR REPORTING
if(isset($_GET['email']) && isset($_GET['token']))
{
//Get variables from email password reset link
$token = $_GET ['token'];
$email = $_GET['email'];
require_once('dbconfig.php');
//CHECK TO SEE IF EMAIL EXISTS IN THE DATABASE//
$sql = "SELECT * FROM users_launchpad WHERE email = '$email' ";
$query = mysqli_query($conn, $sql);
$result = mysqli_fetch_assoc($query);
if ($result['email'] == $email)
{
$check1 = true;
}
else
{
$check1=false;
}
//END EMAIL CHECK//
//CHECKS TO SEE IF ID MATCHES TOKEN//
if(md5($result['id'])==$token)
{
$check2 = true;
}
else
{
$check2 = false;
}
//END ID CHECK
//IF BOTH CHECKS ARE TRUE, CHANGE PASSWORD UPON POST.
if($check1==$check2)
{
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$password = $_POST['password'];
$update_password = "UPDATE users_launchpad SET password = '$password' WHERE email = '$email'";
$password_query = mysqli_query($conn, $update_password);
// header('location: reset_password_success.php');
if ($password_query)
{
$success_message = "Password Updated Successfully";
}
}
}
}
?>
这是HTML:
<?php require_once('header.php');?>
<link href='../css/login.css' rel='stylesheet' type='text/css'>
<link href ='../css/formValidation.min.css' type = 'text/css'>
</head>
<body>
<div class = "col-md-offset-4 col-md-4" id = "loginForm">
<div class = "row">
//CREATE FORM
<form role="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" id = "reset_password">
<fieldset>
<legend>Reset Password</legend>
<div class = "col-md-12 text-center ">
<span class="text-success "><?php if (isset($success_message)) {echo $success_message;} ?></span>
</div>
<br>
<div class="form-group">
<label for="name">New Password</label>
<input type="password" name="password" placeholder="Password" required class="form-control" />
</div>
<div class="form-group">
<label for="name">Confirm New Password</label>
<input type="password" name="confirmPassword" placeholder="Confirm Password" required class="form-control" />
</div>
</fieldset>
<div class="form-group">
<input type="submit" name="reset_password" value="Login" class="btn btn-primary" />
</div>
</form>
<div class = "col-md-6 col-md-offset-3 text-center">
<span class="text-danger "><?php if (isset($errormsg)) { echo $errormsg;} ?></span>
</div>
<br>
<br>
</div>
<input type="hidden" name="token" value= "" />
</div>
//END CREATE FORM
这是Javascript:
//INCLUDE BOOTSTRAP AND FORM VALIDATION
<script src="//oss.maxcdn.com/bootbox/4.2.0/bootbox.min.js"></script>
<script src="js/formValidation.min.js"></script>
<script src="js/framework/bootstrap.min.js"></script>
<script>
$(document).ready(function()
{
$('#reset_password')
.formValidation({
framework: 'bootstrap',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
password: {
validators: {
notEmpty: {
message: 'Your password is required'
},
stringLength: {
min: 4,
max: 30,
message: 'Your password must be between 4 and 30 characters'
},
identical: {
field: 'confirmPassword',
message: 'The password and its confirm are not the same'
}
}
},
confirmPassword: {
validators: {
notEmpty: {
message: 'Your password is required'
},
stringLength: {
min: 4,
max: 30,
message: 'Your password must be between 4 and 30 characters'
},
identical: {
field: 'password',
message: 'The password and its confirm are not the same'
}
}
}
}
})
.on('success.form.fv', function(e)
{
e.preventDefault();
var $form = $(e.target);
$form.get(0).submit();
return false;
});
});
</script>
</body>
</html>
答案 0 :(得分:1)
在您的PHP代码中,您需要发送电子邮件并设置令牌,因为我可以看到令牌未被发送仅请求密码并且正在发送confirmPaswword 。
1-在表单中放置令牌。
2-将表单视图设置为PHP页面,将电子邮件发送为隐藏。
//CREATE FORM
<form role="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" id = "reset_password">
<fieldset>
<legend>Reset Password</legend>
<div class = "col-md-12 text-center ">
<span class="text-success "><?php if (isset($success_message)) {echo $success_message;} ?></span>
</div>
<br>
<div class="form-group">
<label for="name">New Password</label>
<input type="password" name="password" placeholder="Password" required class="form-control" />
</div>
<div class="form-group">
<label for="name">Confirm New Password</label>
<input type="password" name="confirmPassword" placeholder="Confirm Password" required class="form-control" />
</div>
</fieldset>
<div class="form-group">
<input type="submit" name="reset_password" value="Login" class="btn btn-primary" />
</div>
<input type="hidden" name="token" value= "random" />
<input type="hidden" name="email" value= <?= $_GET['email'] />
</form>
<div class = "col-md-6 col-md-offset-3 text-center">
<span class="text-danger "><?php if (isset($errormsg)) { echo $errormsg;} ?></span>
</div>
<br>
<br>
</div>
</div>