我正在创建一个“忘记用户名”页面,您可以在其中输入您的电子邮件地址,并使用jquery在div中显示带有匹配用户名的消息。大多数事情似乎都有效。触发jquery事件,向PHP发送$ .ajax POST。 PHP运行mysqli_query和mysqli_num_rows并根据结果回显一条语句,该语句出现在div中。
什么是无效的是数据库查询和num_rows结果。如果我在没有jquery(通过提交按钮)的情况下运行带有正确回声的php / mysql查询,它们可以正常工作。但是,当使用jquery时,我的mysql停止工作。我已经能够在网站的其他部分运行php / mysql的其他jquery函数。连接和查询语法是正确的,num_rows始终返回null。我很茫然。
代码:
HTML:
<div id="forgotuserPanel" class="panel">
<form action="index.inc.php" method="POST" id="forgotuserForm">
<h2>Please enter your primary email address and your username will be sent to you.</h2>
<input type="email" name="forgot_email" id="forgot_email" size="32" placeholder="Primary Email Address"><br><br>
<button class="submitButton" id="forgot_email_button" name="forgot_email_button">Send Username</button>
</form>
<a class="slideLink" id="forgotPassword" href="#forgotpasswordPanel" name="forgotpasswordBread" title="Forgot Password">Forgot your password? Click here</a><br/>
<span id="usernameResponse"></span>
</div>
jQuery的:
$("#forgot_email_button").on("click", function(e) {
e.preventDefault();
var forgot_email = $("#forgot_email").val();
$.ajax({
type: "POST",
url: "./inc/php/index.inc.php",
data: {
forgot_email: forgot_email
},
async: false,
success: function(data) {
$("#usernameResponse").html(data);
}
});
});
PHP:
if (isset($_POST['forgot_email'])) {
$forgot_email = strip_tags(@$_POST['forgot_email']);
$forgot_username_query = mysqli_query($DB_connect,"SELECT * FROM users WHERE email_primary = '$forgot_email'");
if (mysqli_num_rows($forgot_username_query)) {
while ($row = mysqli_fetch_array($forgot_username_query)) {
$mail_fn = $row["first_name"];
$mail_ln = $row["last_name"];
$usernameto = $row["username"];
$emailto = $row["email_primary"];
}
}
echo (mysqli_num_rows($forgot_username_query) > 0) ? "Thank you, " . $mail_fn. ". Your username is " .$usernameto : "Sorry, this email address was not found.";
}
如果可能的话,我更愿意得到答案,说明代码应该如何重写,而不仅仅是指导我做一些事情。谢谢!