-1

我正在尝试创建一个单页应用程序。成功登录后,将显示一个成功页面(login_success.php),其中包含注销链接。当您单击注销链接时,我希望它清空“主”div,并将 logout.php(“您已注销”页面)插入“主”div。但是,当我单击注销链接时,什么也没有发生。js/application.js

$(document).ready(function() {
    $("#logoutlinkdiv a").click(function(event) {
        event.preventDefault();
        $("#main").empty();
        $("#main").load("templates/logout.php");
        alert("working");
   });
});

警报永远不会被执行......

模板/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>

索引.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();
}
?>
4

4 回答 4

0

想通了...原来我的 application.js 脚本在登录时没有运行 - 我必须将它添加到 login_success.php html 头。有谁知道为什么?

我改变这个:

<?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" > <!--onclick='swap()'> -->
    <a href = "#" >Log out</a>
</div>
</body>
</html>

对此:

<?php
session_start();

if($_SESSION['username'] === null){
header("location: ../index.php");
}
?>

<html>
<script src="js/application.js"></script>
<body>
<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
<div id="logoutlinkdiv" > <!--onclick='swap()'> -->
    <a href = "#" >Log out</a>
</div>
</body>
</html>

有谁知道为什么我必须在此页面上加载脚本?我正在尝试创建一个单页应用程序......所以我不想在整个地方重新加载脚本。

于 2013-06-16T14:24:18.663 回答
0

It's because login_success.php is a seperate static page so you need your jQuery file in there upon redirect.

于 2013-11-25T15:20:18.490 回答
0

在 . 中添加事件function(event)。否则将执行默认的 href="#"。

  $("#logoutlinkdiv a").click(function(event) {
            event.preventDefault();
            ....
    }
于 2013-06-15T17:48:28.213 回答
0

尝试:

    $("#main").load("templates/logout.php", function() {
        alert("working");
 });

确保文件的路径正确

于 2013-06-16T01:20:02.367 回答