为用户提供X在PHP中输入值的机会

时间:2013-06-27 11:56:36

标签: php html forms loops

嗨,我是一个学习PHP的新手(& on stackoverflow) - 我试图解决一个简单的问题,但无法做到。我已经在发布问题之前搜索了谷歌和stackoverflow,因为我不想浪费其他时间,但是一个星期现在无法解决这个问题。

我正在写一个简单的程序在php中,让用户输入一个数字并检查输入的值是否为5.如果为true则回显“你赢了”否则“再试一次”。我能够做到这一点

对我来说棘手的部分是我想给他只有10次机会并尝试使用基本的PHP我无法做到这一点。尝试过使用if,for,while但无法“循环播放html”..我不知道jquery等,我正在尝试用PHP完成这个。我尚未尝试学习课程等。提前致谢

<html>
<body>
TRY AND GUESS THE NUMBER
<br/>
<br/>
<form method="POST" action="gullible.php">
Please enter any number :<input type="text" name="num">
<input type="hidden" name="cnt" value=<?php $cnt=0 ?>>
<input type="submit" name="go">
</body>
</html>
<?php
$i=0;
 if ($_POST['num']!=5)
 {
 $i++;
 echo $i;
 echo " Please try again";
 }
 else 
 echo "You win the game";
 ?>'

5 个答案:

答案 0 :(得分:0)

您需要以某种方式存储变量,使其保持不变。在您的脚本中,每次运行时都会将$i设置为0。另外,您在隐藏输入中错误地设置了值。

这样做的一种方法是使用Session变量,例如$_SESSION['cnt']

我的PHP有点生疏,但这是一个使用Session变量的例子:

$max_guesses = 10;

if( !isset($_SESSION['cnt']) ){
  $_SESSION['cnt'] = 0;
}

if( $_SESSION['cnt']++ > $_max_guesses ){
  echo "Maximum tries exceeded";
} else {
  echo "Try again";
}

如果您不想或不能使用会话变量,您可以使用隐藏的输入字段,就像您尝试过的那样:

<?php
    if( !isset($_POST['cnt']) ){
      $cnt = 0;
    } else {
      $cnt = $_POST['cnt'];
    }

    if( $cnt++ > $_max_guesses ){
      echo "Maximum tries exceeded";
    } else {
      echo "Try again";
    }
?>

<input type='hidden' name='cnt' value='<?php echo $cnt ?>' />

(请注意,如果您的表单使用GET,只需将$_POST替换为$_GET,或者如果您不确定,可以使用$_REQUEST,但最好不要使用。{/ p >

答案 1 :(得分:0)

用户成功登录后,将chances变量设置为10。

$_SESSION['nofchances']=10;

在成功的身份验证页面上设置此标志后。重定向到您的PLAIN HTML代码。

已编辑:

<强> question.html

<html>
<body>
TRY AND GUESS THE NUMBER
<br/>
<br/>
<form method="POST" action="gullible.php">
Please enter any number :<input type="text" name="num">
<input type="submit" name="go">
</body>
</html>

<强> gullible.php

<?php
 if($_SESSION['nofchances']!=0)
 {
 if ($_POST['num']!=5)
 {
 $_SESSION['nofchances'] = $_SESSION['nofchances'] - 1;
 echo "You have ".$_SESSION['nofchances']." no of chances to try";
 echo "<br>Please try again";
 header("location:question.html");
 }
 else
 {
 echo "You won the game";
 $_SESSION['nofchances']=10; // resetting back
 }
 }
 else
 {
 echo "Your chances expired";
 }
 ?>

答案 2 :(得分:0)

如果您不想使用会话,您可以定义一个隐藏的输入字段,该字段存储当前的尝试,然后在按下提交/重新加载网站时启用“+1”。类似的东西:

if( isset($_POST['try']) ) {
   $try = $_POST['try'];
   $try += 1;
} else {
   $try = 0;
}

在表单中添加隐藏字段,如:

$hiddenTry = '<input type="hidden" value="'. $try .'" name="try"/>';

并添加if子句以何时显示如下表单:

if ( $try <= 10 ) {
  //your form
}

答案 3 :(得分:0)

您可以在onBlur / onChange中调用函数

<script>
function test()
{
var count=<?php echo $count;?>;
var guess=parseInt($('#hid_num').val())+1;
if(guess>count)
{
 alert('Your chances over!');
}
else
{
$('#hid_num').val(guess);
}
}
</script>

<input type="text" onblur="test();" id="chk_estimate" />
<input type="hidden" value="0" id="hid_num" /></body>

答案 4 :(得分:0)

我为你做了这个我希望它可以帮助你学习新的东西(我编辑了几次让变量名更容易理解,确保你再次检查它 - 我还添加了作弊:))

<?php
    session_start(); // with this we can use the array $_SESSION to store values across page views of a user.

    mt_srand(time()); // this is to ensure mt_rand function will produce random values. just ignore it for now. it's another story :)

    $max_tries = 10; // limit of guesses

    $_SESSION['the_magic_number']=!isset($_SESSION['the_magic_number'])?mt_rand(0,100):$_SESSION['the_magic_number'];
    // the previous line is a one-liner if then else statement. one-liners works like this: 
    // $my_name_will_be=($isBoy==true)?"George":"Mary";

    if(isset($_POST['num'])) // if this pageview is from a valid POST then...
    {
        $_SESSION['current_try']=isset($_SESSION['current_try'])?$_SESSION['current_try']+1:1;
        // one-line if then else again. This increases the try user is now, or resets it to one
    }
?>  
<html>
    <body>
        TRY AND GUESS THE NUMBER
        <br/>
        <br/>
<?php   
    if ($_SESSION['current_try']<=$max_tries) // if user has more tries available
    {
         if(intval($_POST['num'])==$_SESSION['the_magic_number']) // did he found it?
         {
             echo "You found it! Gongratulations! Click <a href=''>here</a> to try again!";

             // oh and do not forget to reset the variables (you found this bug, well done!)
             $_SESSION['current_try']=1;
             $_SESSION['the_magic_number']=NULL;
         }
         else
         {
             // if he didn't found it, display the status of tries left, and the form to try again
             echo "This is your try ".($_SESSION['current_try'])." of ".$max_tries." Good Luck!";
?>
        <form method="POST" action="mygame.php">
            Please enter any number :
            <input type="text" name="num"/>
            <input type="hidden" name="tries" value="<?php echo (isset($_POST['tries']))?$_POST['tries']-1:$max_tries; ?>"/>
            <input type="submit" name="go"/>
        </form>
        <span style="color:white;background-color:white;"><?php echo "You bloody cheater! The magic number is ".$_SESSION['the_magic_number'];?></span>
<?php
         }
    }
    else
    { 
        // here we are if no tries left! An empty href to reload the page, and we resetting our variables.
        // the_magic_number gets NULL so at the start of script it will be "not set" and will get a mt_rand(0,100) value again
        echo "You lost! Sorry! Click <a href=''>here</a> to try again!";
        $_SESSION['current_try']=1;
        $_SESSION['the_magic_number']=NULL;
    }
?>
    </body>
</html>

最后的跨度是作弊;)按ctrl + a使用它!!!