保持HTML表单的价值

时间:2013-03-06 02:17:47

标签: php html forms

我正在尝试为实体游戏制作一个简单的记分器应用程序,例如拼字游戏。我有两个页面,一个用于询问用户的名字,另一个用于计算他们的分数。我想在分数旁边的第二页上显示用户的名字;例如,Max:47。问题是页面只显示一次用户的名字。有人输入分数后,他们就会消失。我尝试过使用cookies,但它们似乎只是因为没有显示任何东西而变得更糟糕!您可以在ripdvd.x10.mx上访问应用程序,而不是显示名称。有谁知道这方面的简单解决方案?

第一页:     

<html>

<head>
<title>Select Players</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
    <form method="post" action="scoregen.php">
    <p class="par">
        <label for="player1">Please type in the one of the players first name:</label>
            <input type="text" id="player1" name="player1" size="17"  maxlength="17" value="Enter Name" class="textbox" />
    </p>

    <p class="par">
        <label for="player2">Please type in another players first name:</label>
            <input type="text" id="player2" name="player2" size="17"  maxlength="17" value="Enter Name" class="textbox" />
    </p>
        <input type="submit" name="submit" value="Start Playing!" />


    </form>
</body>

</html>

第二页:     

$addScore1 = $_POST['addScore1'];
$addScore2 = $_POST['addScore2'];

$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];

$curr1=$_COOKIE["score1"]+$addScore1;
$curr2=$_COOKIE["score2"]+$addScore2;
setcookie("score1", $_COOKIE["score1"]+$addScore1, time()+3600);
setcookie("score2", $_COOKIE["score2"]+$addScore2, time()+3600);

//Reset cookies if reset button is 't', which makes it clear scores
if ($clse = t){
setcookie ("score1", "", time() - 3600);
setcookie ("score2", "", time() - 3600);
}


// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Score Add</title>
        <link rel=StyleSheet href="style.css" type="text/css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>

    <body>
    <form method="post" action=" ">

     <p class="par">
            <label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
            <input type="text" name="addScore1" id="addScore1" class="textbox" />
            <input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $oldscore1; ?>" />
        <input type="submit" value="Add Score!" />
    </p>

    <p class="par">
        <label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
        <input type="text" name="addScore2" id="addScore2" class="textbox"/>
        <input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
            <input type="submit" value="Add Score!"/>
        </form>
    </p>

         <form method="post" action=" ">
         <input type="hidden" name="clsc" id="clsc" value="t" />
         <input type="submit" value="Clear Scores" />
         </form>
<!--Shows player and score-->
    <p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
    <p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

请勿使用cookies。开始$_SESSION并将信息存储在那里,然后将其从下一页的$_SESSION中回显。

答案 1 :(得分:0)

你可以为所有数据创建表临时文件,所以你选择all,并在使用时创建ajax'$ _session'也是一个好处:D