-2

我正在制作一个类似计算器的 Web 应用程序,它询问用户名,然后在下一页显示他们的名字并添加他们的分数。我有三个问题:

  1. 用户名不会显示在第二页上
  2. 不会加分
  3. 每当您为玩家 1 输入分数时,玩家 2 的分数将重置为零,并且 vise-versia

您可以访问该页面:ripdvd.x10.mx/index.php 提前致谢!

第一页:

<!DOCTYPE html>

<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" 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" class="textbox" />
    </p>
        <input type="submit" class="button" name="button" value="Start Playing!" />


    </form>

</body>

</html>

第二页(显示分数并提供更新它们的方法)

<?php
// Get data from HTML form.
//Gets the player names
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];

$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" class="button" 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" class="button" value="Add Score!"/>
        </form>
         </p>

         <form method="post" action=" ">
         <input type="hidden" name="clsc" id="clsc" value="t" />
         <input type="submit" class="reset" 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>
4

3 回答 3

1

我对您的代码进行了几处更改(包括删除 cookie - 希望没关系)。如果你可以不吃饼干,试试这个:

<?php
// Get data from HTML form.
// Gets the player names
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];

if ($_POST['addScore1'] == null)
    $addScore1 = 0;
else
    $addScore1 = $_POST['addScore1'];

if ($_POST['addScore2'] == null)
    $addScore2 = 0;
else
    $addScore2 = $_POST['addScore2'];

if ($_POST['oldScore1'] == null)
    $oldScore1 = 0;
else
    $oldScore1 = $_POST['oldScore1'];

if ($_POST['oldScore2'] == null)
    $oldScore1 = 0;
else
    $oldScore2 = $_POST['oldScore2'];

$curr1=$oldScore1+$addScore1;
$curr2=$oldScore2+$addScore2;

if ($_POST['clse'] == "t"){
    $curr1 = 0;
    $curr2 = 0;
}

// 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="<?php echo $_SERVER['PHP_SELF'];?>">
            <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 $curr1; ?>" />
                <input type="submit" class="button" 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 $curr2; ?>" />
                <input type="submit" class="button" value="Add Score!"/>
            </p>
            <input type="hidden" name="player1" value="<?PHP echo $player1;?>" />
            <input type="hidden" name="player2" value="<?PHP echo $player2;?>" />
        </form>

        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            <input type="hidden" name="clsc" id="clsc" value="t" />
            <input type="submit" class="reset" 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>

请注意,我实际上并没有运行它,但请尝试一下,如果您遇到任何错误或仍有问题,请告诉我。

于 2013-03-07T01:23:56.243 回答
1

试试这个:

索引.php

    <!DOCTYPE html>

<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" 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" class="textbox" />
    </p>
        <input type="submit" class="button" name="names" value="Start Playing!" />


    </form>

</body>

</html>

scoregen.php

<?php
    // Get data from HTML form.
    //Gets the player names
    if(isset($_POST['player1'])){
        $player1 = $_POST['player1'];
    }else{
        $player1 = "No Player"; 
    }   

    if(isset($_POST['player2'])){
        $player2 = $_POST['player2'];
    }else{
        $player2 = "No Player"; 
    }   

    //CHECKS IF THE ADDSCORE BUTTON IS HIT
    if(isset($_POST['AddScore'])){  
        $oldScore1 = $_POST['oldScore1'];
        $oldScore2 = $_POST['oldScore2'];

        if(!empty($_POST['addScore1'])){
            $addScore1 = $_POST['addScore1'];
        }else{
            $addScore1 = 0;
        }

        if(!empty($_POST['addScore2'])){
            $addScore2 = $_POST['addScore2'];
        }else{
            $addScore2 = 0;
        }

    }else{//INITIALIZES THE SCORES TO ZERO; OR SETS IT TO ZERO IF THE CLEAR SCORES BUTTON IS HIT
        $oldScore1 = 0;
        $oldScore2 = 0;
        $addScore1 = 0;
        $addScore2 = 0;

    }

    $curr1 = $oldScore1 + $addScore1;
    $curr2 = $oldScore2 + $addScore2;

    // 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 $curr1; ?>" />
            <input type="submit" name="AddScore" class="button" 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 $curr2; ?>" />
            <input type="submit" name="AddScore" class="button" value="Add Score!"/>       
        </p>
                <input type="hidden" name="player1" value="<?php echo $player1;?>"/>
                <input type="hidden" name="player2" value="<?php echo $player2;?>"/>
        <p class="par">
              <input type="submit" class="reset" value="Clear Scores" />
        </p>

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

1)我在这里没有使用cookies,你实际上不需要它来记录分数,你可以用它来保存用户名。

2) 你实际上可以只有一个 addScore 按钮,但在这段代码中它并不重要。无论如何它都会添加分数。

3)我总是检查是否设置了 POST 的一个元素或者它是否有一个值。所以我检查了分数和名字。

4)在您的代码的这一行中: <input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" /> 我将 更改$oldscore$curr,因为在下一次提交中需要 $curr 分数作为 oldscore ,因此分数会加起来。

于 2013-03-07T02:41:01.277 回答
0

$oldScore1不相等$oldscore1

并且无论如何您都需要保留值以发布到下一页

<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />

放下饼干,然后使用

$curr1=$oldScore1+$addScore1;
$curr2=$oldScore2+$addScore2;

检查帖子是否设置为避免警告也是值得的,并确保它们是整数,例如

$oldScore1 = isset($_POST['oldScore1'])? (int)$_POST['oldScore1'] : 0;    
$oldScore2 = isset($_POST['oldScore2'])? (int)$_POST['oldScore2'] : 0;
于 2013-03-07T01:22:15.760 回答