我正在尝试创建一个单页应用程序。成功登录后,将显示一个成功页面(login_success.php),其中包含注销链接。当您单击注销链接时,我希望它清空“主”div,并将 logout.php(“您已注销”页面)插入“主”div。但是,当我单击注销链接时,什么也没有发生(似乎 jquery 没有被执行)。我不确定如何处理链接中的“href” - 因为我不希望页面实际更改。这是我的代码:
js/application.js
$("logoutlinkdiv").click(function() {
$(this).find('a').click(function ()) {
$("#main").empty();
$("#main").load("logout.php");
});
});
模板/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>
更新
现在我有这个代码 - 当我点击“注销”时它仍然什么都不做:
模板/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");
});
});
索引.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>
检查登录.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();
}
?>
同样,当我从“login_success.php”中单击“注销”时,除了将“#”附加到 url 之外什么都没有发生(这应该发生,因为它是链接的占位符)。有什么建议么?