2013-03-07 44 views
-2

我正在制作一个类似计算器的Web应用程序,要求输入用户名,然后在下一页显示他们的名字并添加他们的分数。我有三个问题:使用HTML表单和php

  1. 名称将不会在第二页上显示的用户
  2. 它不会当你输入一个分数,供玩家1,玩家加分值
  3. 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> 
+0

只需一次快速观察,无需彻底查看代码。 'if($ clse = t){'需要更改为'if($ _POST ['clse'] ==“t”){'... – 2013-03-07 01:12:22

+0

尝试隔离问题。减少代码,直到找到导致问题的部分。下次只发布你有问题的部分,而不是整个代码转储。 – kapa 2013-03-07 01:14:44

+0

popnoodles,不,你不要 – 2013-03-07 01:15:09

回答

1

我做了一些改动你的代码(包括取消饼干 - 希望这是确定)。如果你生活中可以没有饼干,试试这个:

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

注意,我还没有真正跑这一点,但尝试,让你得到任何错误或仍然有问题,我知道了。

+0

谢谢!它完美的工作! – 2013-03-07 12:32:41

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; 
1

试试这个:

的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="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)我没有在这里使用cookie,你实际上并不需要它的分数,你可以用它来保存用户名。

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

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

4)在这行代码中的: <input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" /> 我改变了$oldscore$curr,因为会在未来需要的$ CURR分数提交的oldscore这样的比分会积少成多。