duanping5306 2013-06-18 19:23
浏览 29
已采纳

得到var_dump()但没有jquery动作/登录系统

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;
    var_dump($_SESSION['username']);
}
else 
{
    echo "Wrong Username or Password";
    session_destroy();
}
?>

The above script runs when someone logs in using this form:

templates/indexforms.php

<?php
session_start(); 
?>

<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>

The above template gets loaded into index.php (below). I am trying to write an SPA so everything is being loaded and unloaded into the "main" div using jquery.

index.php

<?php session_start(); ?>

<!DOCTYPE html>
<html>
<head>
<title>it IT</title>
    <script src="reqscripts/jquery.js" type="text/javascript"></script>
    <script src="js/application.js" type="text/javascript"></script>
</head>
<body>
    <div id="main"></div>
</body>
</html>

Here is the jquery/ajax code which loads and unloads elements:

js/application.js

$(document).ready(function() {
    $("#main").load("templates/indexforms.php", function () {
        //When login form is submitted
        $("#login").submit(function(e) {
            e.preventDefault();
            $.post("checklogin.php", $(this).serialize(), function() { //ajax post call
                $("#main").load("templates/login_success.php"); //load template
                $("#login").remove(); //remove form
            });
        });

        $(document).on("click", "#logoutlinkdiv a", function(e) {
            e.preventDefault();
            $("#main").empty();
            $("#main").load("templates/logout.php");
        });

        $(document).on("click", "#loginlinkdiv a", function(e) {
            e.preventDefault();
            $("#main").empty();
            $("#main").load("templates/indexforms.php");
        });
    });
});

After login, the jquery loads a "login_success.php" template:

templates/login_success.php

<?php
session_start();
?>

<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
<div id="logoutlinkdiv" >
    <a href = "#" >Log out</a>
</div>

If you click the "logoutlinkdiv" link, the jquery loads a template "logout.php":

templates/logout.php

<?php
session_start();
session_destroy();
?>

<div id="loginlinkdiv" >
    <h2>You have been logged out.</h2>
    <a href = "#">Log in</a>
</div>

If you click the "Log in" link on the above template...the indexforms.php template gets loaded (above). Then, I try to login again. The checklogin.php script gets run and 'string(4) "obey"' gets displayed. This makes sense because I did a var_dump() for the username (obey) in the if block of checklogin.php. However, it seems like the jquery doesn't get executed...because the login_success.php page never loads. If I reload "index.php" and login again...it works - Basically, I don't understand why the flow of my program is acting like this.

UPDATE

Here is what I see in the firebug console under the NET tab. A bunch of requests stack up - until I go to login for a second time. Then, the requests clear...and only one request remains:

I load the index page and I see:

GET http://localhost/~Eamon/ 200 OK
GET http://localhost/~Eamon/templates/indexforms.php 200 OK

I click the login link and see:

POST http://localhost/~Eamon/checklogin.php 200 OK
GET jquery.js 304 Not Modified
GET application.js 304 Not Modified
GET http://localhost/~Eamon/templates/login_success.php 200 OK

I click the logout link and see:

GET http://localhost/~Eamon/templates/logout.php 200 OK

Then I click the link that brings me to the login page again, and I see:

GET http://localhost/~Eamon/templates/indexforms.php 200 OK

Then, I enter my info and log in again and all the requests clear, and I only see:

POST http://localhost/~Eamon/checklogin.php 200 OK

I think I should be seeing:

POST http://localhost/~Eamon/checklogin.php 200 OK
GET jquery.js 304 Not Modified
GET application.js 304 Not Modified
  • 写回答

1条回答 默认 最新

  • doumo2501 2013-06-18 22:27
    关注

    After a little research, I think I fixed it...I had to change my jquery to this:

    ...
    
    $(document).on("click", "#loginlinkdiv a", function(e) {
        e.preventDefault();
        location.reload();
        $("#main").load("templates/indexforms.php");
    });
    
    ...
    

    I don't think that application.js was being executed (on second login) because index.php didn't reload the script upon clicking the "loginlinkdiv" link...the only way I could think to do this was to use location.reload(); which I think is reloading index.php - and the script is being read again. Anyone who can clarify why this works (or maybe suggest a better answer) gets kudos.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog