doubeng1278 2017-03-27 13:59
浏览 118

.load()在登录时不执行重新加载导航栏

After the user logs in, I want the nav bar to change its "Log in" button to a "Log Out" button. After the user submits the correct username and password, I called toggleLogInButton(), which calls .load() to reload the nav bar. The "Congratulations" message shows up, but the log in button doesn't change. How do I fix this?

login.php

<head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>  
        <script src="js/main.js"></script>
</head>

<?php include "includes/navbar.php" ?> 
//some more code here
<?php
    if( password_verify( $password, $db_hashed_password ) ) {
        $_SESSION['logged_user'] = $username;
        echo ("Congratulations, $username! You logged in successfully!");
        echo "<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js' type='text/javascript'>toggleLogInButton();</script>";
    }
?>

main.js

$(document).ready(function(){
    /*Reload Log In button for nav bar */
    function toggleLogInButton() {
        $( ".right" ).load( "../includes/navbar.php .right" );
    }

});

navbar.php

<?php

echo ('<div class = "navBarWrapper">
        <div class="top bar">
            <ul class = "right float_right">');

            if (isset($_SESSION['logged_user'])) {
                echo("<li class = 'buttons'> <a href = 'logout.php' class='otherPages'>Log Out</a></li>");
            } else {
                echo("<li class = 'buttons'> <a href = 'login.php' class='otherPages'>Login</a></li>");
            }

echo(       '</ul>
        </div> 
</div>');
?>
  • 写回答

1条回答 默认 最新

  • dongxian2863 2017-03-27 14:27
    关注

    In login.php you're outputting script with source 'jquery'. It cause double jquery script on the page. Remove src from here and wrap function in ready statement:

    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
        <script src="js/main.js"></script>
    </head>
    
    <?php include "includes/navbar.php" ?>
        //some more code here
    <?php
    if (password_verify($password, $db_hashed_password)) {
        $_SESSION['logged_user'] = $username;
        echo("Congratulations, $username! You logged in successfully!");
        echo "<script type='text/javascript'>$(document).ready(function() { toggleLogInButton();})</script>";
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘