I'm working on a user login system using jQuery UI modal dialogs to interact between my html and PHP scripts. Recently, I moved all of my user functionality to one PHP file, from where I had them before in my header markup.
I was able to get the login functionality to work if someone puts in a valid username/password, but now I am trying to account for other possibilities, i.e. someone enters a valid username but incorrect password, or if they enter a username that does not exist.
The problem I'm having is with the AJAX call on the Dialog script. I want to submit the login form to the same page, user.php, and either log a user in, or return an error message that would populate a div on the same dialog.
Right now, my code will return an error message, but it also returns a copy of the user form. Not really sure what I am doing wrong here...
Any input would be appreciated.
EDIT: Decided to go ahead and post the entirety of user.php, since making changes based on one answer made the html added by this file disappear entirely. I'm now guessing that there are probably several things I'm doing wrong:
<html>
<span id="user">
<ul>
<?php
//if user is logged in, show name and Log Out option, else show Log In and Sign Up options
if(isset($_COOKIE["user"])) {
setSessionUserData($_COOKIE["user"]);
echo "<li>Logged in as {$_SESSION["username"]}</li>";
echo "<li> | </li>";
echo "<li><a href='#' id='logout_link'>Log Out</a></li>";
} else {
echo "<li><a href='#' id='login_link'>Log In</a></li>";
echo "<li> | </li>";
echo "<li><a href='#'>Sign up</a></li>";
}
?>
</ul>
</span>
<div id="login_dialog" title="Existing User">
<form id="login_form">
<fieldset>
<label for="userid">Username</label>
<input type="text" name="username" id="username" />
<label for="pw">Password</label>
<input type="password" name="pw" id="pw" />
</fieldset>
</form>
<span class = "error"></span>
</div>
<div id="logout_dialog" title="Log Out">
<p><span class="ui-icon ui-icon-alert alert-icon"></span>Are you sure you want to log out?</p>
</div>
<script>
$(function() {
var username = $("#username"),
password = $("#pw"),
loginFields = $([]).add(username).add(password);
$("#login_link").click(function() {
$("#login_dialog").dialog("open");
});
$("#logout_link").click(function() {
$("#logout_dialog").dialog("open");
});
$("#login_dialog").dialog({
autoOpen: false,
height: 200,
width: 250,
resizeable: false,
modal: true,
buttons: {
Submit: function() {
$.post("auth.php",
{
username: $("#username").val(),
password: $("#pw").val()
},
function(result) {
if(result=='success'){
window.location='Practice.php';
}
else {
$(".error").html(result);
}
}
)
},
Cancel: function(){
$(this).dialog("close");
}
},
close: function() {
loginFields.val("");
}
});
$("#logout_dialog").dialog({
autoOpen: false,
modal: true,
height: 150,
resizeable: false,
buttons: {
Okay: function() {
window.location = "logout.php";
},
Cancel: function() {
$(this).dialog("close");
}
}
});
});
</script>
</html>
<?php
function setSessionUserData($cookie) {
$con = connect();
$query = "SELECT * FROM practice_user WHERE userid = '$cookie'";
$result = mysqli_query($con, $query);
$userData = mysqli_fetch_array($result);
$_SESSION["username"] = $userData["username"];
$_SESSION["fname"] = $userData["first_name"];
$_SESSION["lname"] = $userData["last_name"];
}
?>
...and the PHP:
if($_SERVER["REQUEST_METHOD"] == "POST") {
$user = test_input($_POST["username"]);
$pass = test_input($_POST["password"]);
//check if correct login
if (chkLogin($user, $pass)) {
$id = getUser($user, $pass);
setUserCookie($id);
header("Location: Practice.php");
}
//check if username exists but incorrect password entered
else if (chkUser($user)) {
echo "Username and password do not match";
}
//Or else return that username doesn't exist
else {
echo "Username does not exist";
}
}
There are other functions in my code to handle validation and such.
If I need to post those, please let me know.