当我重新加载页面值不变时,它们仅在我登录时才更改

时间:2019-04-20 13:51:18

标签: javascript php html

因此,我希望当用户重新加载页面时,其统计信息根据数据库进行更改。 例如,假设我刚刚注册,我的统计数据是Money-0,Diamond-0,Ruby-0,然后以某种方式向用户添加了10笔钱。 现在,我要的是用户只有在决定重新加载页面后才能看到他的新统计信息(Money-10,Diamond-0,Ruby-0)。

我已经在主体上添加了onload函数,但是它似乎不起作用。 用户统计信息只有在用户注销然后再次登录时才会更改。

index2.php(应在其中显示用户统计信息的地方)

<?php include('server.php') ?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>PwettyKittyPincesa</title>

  <link href="./style.css" type="text/css" rel="stylesheet" />

  <script>
    function getUserStats(){
        <?php
            $queryThree = "SELECT * FROM `register` WHERE username='$username'";
            $userStats = mysqli_query($db,$queryThree);
            while($userStatsTwo = mysqli_fetch_assoc($userStats)){
                $_SESSION['userid'] = $userStatsTwo['ID'];
                $_SESSION['username'] = $userStatsTwo['username'];
                $_SESSION['diamonds'] = $userStatsTwo['diamonds'];
                $_SESSION['ruby'] = $userStatsTwo['ruby'];
                $_SESSION['money'] = $userStatsTwo['money'];
                $_SESSION['level'] = $userStatsTwo['level'];
            };
        ?>
    }
  </script>
</head>
<body onload="getUserStats()">
    <div class="navWrapper">
        <div class="statistics">
            <div class="profilePicture" name="profilePicture">
                <label class="profilePictureLabel" for="profilePicture"><b><?php echo $_SESSION['username']; ?></b></label>
            </div>

            <div class="money" name="money">
                <label class="rubyLabel" for="ruby"><b><?php echo $_SESSION['money']; ?></b></label>
            </div>

            <div class="diamond" name="diamond">
                <label class="diamondLabel" for="diamond"><b><?php echo $_SESSION['diamonds']; ?></b></label>
            </div>

            <div class="ruby" name="ruby">
                <label class="rubyLabel" for="ruby"><b><?php echo $_SESSION['ruby']; ?></b></label>
            </div>

            <div class="level" name="level">
                <label class="levelLabel" for="level"><b>Level:<?php echo $_SESSION['level']; ?></b></label>
            </div>
        </div>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在这种情况下,您不需要任何JavaScript即可实现此目的。您可以在文档顶部运行所有会话分配。另外,您需要在文档的最顶部包含session_start(),以确保会话变量成为全局值。但是,如果不需要在其他页面上使用这些值,则可以使用标准变量来存储数据,而不必将其存储在会话中。

这正在使用会话变量:

<?php
session_start();

include('server.php');

$queryThree = "SELECT * FROM `register` WHERE username='$username'";
$userStats = mysqli_query($db,$queryThree);
while($userStatsTwo = mysqli_fetch_assoc($userStats)){
  $_SESSION['userid'] = $userStatsTwo['ID'];
  $_SESSION['username'] = $userStatsTwo['username'];
  $_SESSION['diamonds'] = $userStatsTwo['diamonds'];
  $_SESSION['ruby'] = $userStatsTwo['ruby'];
  $_SESSION['money'] = $userStatsTwo['money'];
  $_SESSION['level'] = $userStatsTwo['level'];
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>PwettyKittyPincesa</title>

  <link href="./style.css" type="text/css" rel="stylesheet" />
</head>
<body>
    <div class="navWrapper">
        <div class="statistics">
            <div class="profilePicture" name="profilePicture">
                <label class="profilePictureLabel" for="profilePicture"><b><?php echo $_SESSION['username']; ?></b></label>
            </div>

            <div class="money" name="money">
                <label class="rubyLabel" for="ruby"><b><?php echo $_SESSION['money']; ?></b></label>
            </div>

            <div class="diamond" name="diamond">
                <label class="diamondLabel" for="diamond"><b><?php echo $_SESSION['diamonds']; ?></b></label>
            </div>

            <div class="ruby" name="ruby">
                <label class="rubyLabel" for="ruby"><b><?php echo $_SESSION['ruby']; ?></b></label>
            </div>

            <div class="level" name="level">
                <label class="levelLabel" for="level"><b>Level:<?php echo $_SESSION['level']; ?></b></label>
            </div>
        </div>
    </div>
</body>
</html>

这是使用变量:

<?php
include('server.php');

$queryThree = "SELECT * FROM `register` WHERE username='$username'";
$userStats = mysqli_query($db,$queryThree);
while($userStatsTwo = mysqli_fetch_assoc($userStats)){
  $userid = $userStatsTwo['ID'];
  $username = $userStatsTwo['username'];
  $diamonds = $userStatsTwo['diamonds'];
  $ruby = $userStatsTwo['ruby'];
  $money = $userStatsTwo['money'];
  $level = $userStatsTwo['level'];
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>PwettyKittyPincesa</title>

  <link href="./style.css" type="text/css" rel="stylesheet" />
</head>
<body>
    <div class="navWrapper">
        <div class="statistics">
            <div class="profilePicture" name="profilePicture">
                <label class="profilePictureLabel" for="profilePicture"><b><?php echo $username; ?></b></label>
            </div>

            <div class="money" name="money">
                <label class="rubyLabel" for="ruby"><b><?php echo $money; ?></b></label>
            </div>

            <div class="diamond" name="diamond">
                <label class="diamondLabel" for="diamond"><b><?php echo $diamonds; ?></b></label>
            </div>

            <div class="ruby" name="ruby">
                <label class="rubyLabel" for="ruby"><b><?php echo $ruby; ?></b></label>
            </div>

            <div class="level" name="level">
                <label class="levelLabel" for="level"><b>Level:<?php echo $level; ?></b></label>
            </div>
        </div>
    </div>
</body>
</html>