dongshanni1611 2014-05-21 10:37
浏览 37
已采纳

无需手动刷新页面即可将登录更改为注销

I have some issues with my login progress. It logins but it doesn't change the login button to a logout button without manually refreshing the page.

I have a one page jquery plugin that makes the page compact although it doesn't isolate each script from each section.

Here's my page; http://adam-norbacker.ostersjo.nu/

Username: test Password: password

now here's the plugins and scripts that makes sense about the problem: https://github.com/davist11/jQuery-One-Page-Nav

Login/logout button:

<div id="menu" class="default">
<ul id="nav">
<?php 
session_start();
if(isset($_SESSION['username'])){
    $content_hash_name = $_GET['menu'];
    if($content_hash_name == 'section-8');
    echo "<li><a href='logout.php' class='external'>logout</a></li>";
}else{
    echo "<li><a href='#section-8'>login</a></li>";
        }
    ?>
</ul>
</div>

Login script:

  <?php 
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {

$username = trim(htmlentities(mysql_real_escape_string($_POST['username'])));
$password = trim(htmlentities(mysql_real_escape_string($_POST['password'])));
if (!empty($username) && !empty($password)) {
    $_SESSION['username'] = $username;
    echo "<br/> welcome ", $username;
     } else {
    echo "Please enter correct username or password";
     }   
} else {
    echo "please Login";
}
?>
<h1>Login</h1>
<form action="" onSubmit="" method="post">
<label>User Name :</label>
<input type="text" name="username" /><br />
<label>Password</label>
<input type="password" name="password" /><br />
<input type="submit" value="Login" name="submit"/>
</form>
</div>
  • 写回答

3条回答 默认 最新

  • doupixian1436 2014-05-21 11:37
    关注

    The only way to do it is using Javascript, because the PHP code will already be parsed.

    The easiest way is to use jQuery and do an AJAX call to a PHP page. Here's the code:

    <script type="text/javascript">
    $(document).ready(function(){
    
        $("#loginBtn").click(function(){
            var username = $("#username").val();
            var password = $("#password").val();
            $.ajax({
                url: 'login.php',
                type: 'POST',
                data:  {username: username, password: password},
                dataType: 'json',
                success: function(response){
                    // Success
                    $("#loginBtn").hide();
                    $("#logoutBtn").show();
                },
                error: function(response){
                    // Error
                }          
            });
            return false;
        });
    
        $("#logoutBtn").click(function(){
            $("#logoutBtn").hide();
            $("#loginBtn").show();
            return false;
        });
    
    });
    </script>
    
    <input type="text" value="" id="username" />
    <input type="password" value="" id="password" />
    <button id="loginBtn">Login</button>
    <button id="logoutBtn" style="display:none;">Logout</button>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合