I am trying to create a single page app. After a successful login, a success page is displayed (login_success.php) with a link to logout. When you click the logout link, I want it to empty the "main" div, and insert logout.php (a "you have been logged out" page) into the "main" div. However, when I click the logout link, nothing happens (it seems like the jquery isn't getting exectuted). I'm not sure what to do with the "href" in the link - as I do not want the page to actually change. Here is my code:
js/application.js
$("logoutlinkdiv").click(function() {
$(this).find('a').click(function ()) {
$("#main").empty();
$("#main").load("logout.php");
});
});
templates/login_success.php
<?php
session_start();
if($_SESSION['username'] === null){
header("location: ../index.php");
}
?>
<!DOCTYPE html>
<html>
<body>
<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
<div id="logoutlinkdiv">
<a href = "#">Log out</a>
</div>
</body>
</html>
UPDATE
Now I have this for code - it still does nothing when I click "Log out":
templates/login_success.php
<?php
session_start();
if($_SESSION['username'] === null){
header("location: ../index.php");
}
?>
<!DOCTYPE html>
<html>
<body>
<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
<div id="logoutlinkdiv">
<a href = "#">Log out</a>
</div>
</body>
</html>
js/application.js
$(document).ready(function() {
$("#logoutlinkdiv a").click(function() {
event.preventDefault();
$("#main").empty();
$("#main").load("templates/logout.php");
});
});
index.php
<!DOCTYPE html>
<html>
<head>
<title>it IT</title>
<script src="reqscripts/jquery.js"></script>
<script src="js/application.js"></script>
</head>
<body>
<form id="login" method="post" action="checklogin.php">
<h1>Member Login</h1>
<p>Username:<input name="myusername" type="text" id="myusername"></p>
<p>Password:<input name="mypassword" type="password" id="mypassword"></p>
<input type="submit" name="Submit" value="Login">
</form>
<div id="main"></div>
</body>
</html>
checklogin.php
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="bonjour3"; // Mysql password
$db_name="itit"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "$username", "$password", "$db_name")or die("cannot connect");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql = $mysqli->prepare("SELECT * FROM $tbl_name WHERE username=? and password=?");
$sql->bind_param('ss',$myusername,$mypassword);
$sql->execute();
$sql->store_result();//apply to prepare statement
$numRows = $sql->num_rows;
if($numRows === 1){
$_SESSION['username'] = $myusername;
}
else {
echo "Wrong Username or Password";
session_destroy();
}
?>
Again, when I click on "Log out" from within "login_success.php", nothing happens except that a "#" is appended to the url (which should happen because it is the placeholder for the link). Any suggestions?