0

嗨,我是一个学习 PHP 的新手(也在 stackoverflow 上)-我正在尝试解决一个简单的问题,但无法解决。在发布问题之前,我已经在 google 和 stackoverflow 上进行了搜索,因为我不想浪费其他时间,但是现在已经有一周无法解决这个问题了。

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

对我来说棘手的部分是我只想给他 10 次机会并尝试,因为我可能使用基本的 PHP 无法做到这一点。尝试过使用 if, for, do 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";
 ?>'
4

5 回答 5

0

用户成功登录后,像这样将机会变量设置为 10。

$_SESSION['nofchances']=10;

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

编辑:

问题.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>

易受骗的.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";
 }
 ?>
于 2013-06-27T12:11:05.337 回答
0

如果您不想使用会话,您可以定义一个隐藏的输入字段来存储当前的尝试,然后在按下提交/重新加载站点时添加“+1”。就像是:

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

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

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

并在 when 中添加 if 子句以显示如下形式:

if ( $try <= 10 ) {
  //your form
}
于 2013-06-27T12:12:21.750 回答
0

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

一种方法是使用 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如果您不确定可以使用,但最好不要使用。

于 2013-06-27T12:06:09.020 回答
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>
于 2013-06-27T13:10:18.720 回答
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 使用它!

于 2013-06-27T13:34:58.190 回答