当您想要重置密码以仅输入电子邮件时,我一直在尝试删除输入用户名的必要性。我无法理解这里的区别是什么因为我已经删除了html中的用户名部分,然后它告诉我必须填写所有字段,即使我不需要用户名字段,如果我添加它回来然后它应该工作,即使我的PHP代码不需要用户名。
旧的重置表格
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal" class="modal fade">
<form method="POST">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Forgot Password ?</h4>
</div>
<div class="modal-body">
<p>Enter your user name below.</p>
<input type="text" id="username" name="username" placeholder="User Name" class="form-control placeholder-no-fix" autocomplete="off" required>
</div>
<div class="modal-body">
<p>Enter your e-mail address below.</p>
<input type="email" id="email" name="email" placeholder="Email" class="form-control placeholder-no-fix" autocomplete="off" required>
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
<button id="reset" class="btn btn-success" type="button">Reset</button>
<hr>
<div id="result"></div>
</div>
</div>
</div>
</form>
</div>
新的重置表格
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal" class="modal fade">
<form method="POST">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Forgot Password ?</h4>
</div>
<div class="modal-body">
<p>Enter your e-mail address below.</p>
<input type="email" id="email" name="email" placeholder="Email" class="form-control placeholder-no-fix" autocomplete="off" required>
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
<button id="reset" class="btn btn-success" type="button">Reset</button>
<hr>
<div id="result"><?php print_r($_POST)?></div>
</div>
</div>
</div>
</form>
</div>
PHP代码:
if(isset($_POST['action']) && $_POST['action'] == 'reset') {
if(isset($_POST['email'])
&& is_string($_POST['email'])
&& !empty($_POST['email'])) {
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$email = $_POST['email'];
$stmt = $pdo->prepare('SELECT * FROM users WHERE UserEmail = :UserEmail');
$stmt->execute(array(':UserEmail' => $email));
if($stmt->rowCount() > 0) {
$new_password = substr(md5(rand(1,100000)), 0, 8);
$stmt = $pdo->prepare('UPDATE users SET UserPassword = :UserPassword WHERE UserEmail = :UserEmail');
$stmt->execute(array(':UserPassword' => md5($new_password), ':UserEmail' => $email));
$subject = 'Password recovery';
$txt = 'Your account password has been reset.';
$txt .= 'Your new account password is: '.$new_password.'';
$headers = "From: ".$RecoveryEmail."" . "\r\n" .
"CC: ".$RecoveryEmail."";
mail($email,$subject,$txt,$headers);
} else {
echo('User with these credentials does not exist.');
}
} else {
echo('The entered E-mail is invalid.');
}
} else {
echo('Fill all fields correctly.');
}
}