我正在创建登录信息,但我遇到了问题。如果用户输入错误的用户名和/或密码,我希望显示一条错误消息,特别是如果两者都不匹配数据库中的内容,则会提示用户他们输入了错误的用户名/密码
每当我运行代码时,似乎忽略了我的else语句。我的if语句有效,我已经测试过了。但无论我在else子句中回应什么,它都没有被访问。我已经尝试重组php绑定的js,我尝试过类似问题的Stack Overflow解决方案无济于事。我通过逐步测试代码并在if语句为真时测试了代码" sas"如果不是真的,那么就会被回应(否则)""是的回应。控制台上没有错误消息。也许我在这里尝试的逻辑存在问题。
我使用Malsup的jQuery表单插件以异步方式提交登录表单。我已经减少了一些非必要的(我可能知道的更多)html和js,但是包含了很好的测量文件。
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= @report.date %>
</p>
<p>
<strong>Site:</strong>
<%= @report.site_id %>
</p>
<p>
<strong>Created By:</strong>
<%= @report.user_id %>
</p>
<p>
<strong>Type of report:</strong>
<%= @report.type_of_report %>
</p>
<p>
<strong>Type of incident:</strong>
<%= @report.type_of_incident %>
</p>
<p>
<strong>Report:</strong>
<%= @report.report %>
</p>
<%= link_to 'Edit', edit_report_path(@report) %> |
<%= link_to 'Back', reports_path %>
<?php
$host = "meh"; // Host name
$username = "blah"; // Mysql username
$password = "pft"; // Mysql password
$db_name = "doh"; // Database name
$tbl_name = "leh"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "$username", "$password", "$db_name");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
// username and password sent from form
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysqli_real_escape_string($mysqli, $myusername);
$mypassword = mysqli_real_escape_string($mysqli, $mypassword);
$sql = "SELECT FirstName, Password FROM $tbl_name WHERE FirstName= ? AND Password= ? ";
//prepare and bind paramters
if ($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param("ss", $myusername, $mypassword);
$stmt->execute();
$stmt->bind_result($returnedusername, $returnedpassword);
//fetch $stmt results
while ($stmt->fetch()) {
if (isset($returnedusername) && isset($returnedpassword) && $returnedusername === $myusername && $returnedpassword === $mypassword) {
session_register("myusername");
session_register("mypassword");
//header("location:login_success.php");
echo "sas";
} else {//I DONT THIS PART IS BEING RUN...NOT SURE THOUGH IF LOOKING AT WRONG PART OF CODE
echo $_POST['loginError'];//loginError found in html, will be error message
}
} //end while
} //end if
$stmt->close();
$mysqli->close();
?>
$('document').ready(function() {
var loginForm = $("#form1");
//Will load user's name into page heading (from local storage)
function storeUsername() {
window.alert("You made it in the function");//testing to see if code is run
if (typeof(Storage) != "undefined") {
// Store the user's first name as localStorage Object
var inputUsername = document.getElementById("myusername");
localStorage.username = inputUsername.value;
window.alert(localStorage.username);
}
}
var options = {
target: '#loginError',
beforeSubmit: beforeSubmit, // pre-submit callback
success: afterSuccess
};
//Submit form via ajax
// bind to the form's submit event
$('#form1').submit(function() {
// inside event callbacks 'this' is the DOM element so we first
// wrap it in a jQuery object and then invoke ajaxSubmit
$(this).ajaxSubmit(options);
return false;
});
//after succesful upload
function afterSuccess() {
var flag = $("#loginError");
var flagContents = flag.html();
if (flagContents == "sas") {
window.location.href = "login_success.php";
//remove the imgPlaceHolder element
} else {
flag.fadeIn('slow');//error message will fade In
}
}
function beforeSubmit() {
//check whether browser fully supports all File API
if (window.File && window.FileReader && window.FileList && window.Blob) {
storeUsername();
} else {
//Output error to older unsupported browsers that doesn't support HTML5 File API
alert("Please upgrade your browser, because your current browser lacks some new features we need!");
return false;
}
}
});
答案 0 :(得分:0)
这就是问题所在:
<div id="loginError" name="loginError" style="display:none" value="**Username or password incorrect**"></div>
将其更改为:
<input id="loginError" name="loginError" style="display:none" value="**Username or password incorrect**" type="hidden" />