PHP表单 - 将设置值与用户输入值进行比较

时间:2017-02-26 16:08:35

标签: php forms validation variables

我在为赌场经销商创建培训工具的早期阶段。我的代码工作率约为75%。

该工具非常简单:代码生成5到100之间的随机下注量,生成2到12之间的随机骰子数量,根据下注和$winning_bet值计算$factor数量,用户在文本字段中输入答案,代码会通过if-else语句让您知道它是对还是错。

以下是我遇到的问题:

  1. 当我第一次在浏览器中加载文件时,我得到一个"未声明的值" $dealer_answer
  2. 的错误
  3. 即使回应正确答案(以确保我知道我的数学是正确的)并在文本字段中输入正确的答案,我仍然得到一个"这是不正确的。"响应。 (注意:我认为这是因为它使用的是最后保存的值,而不是用户当前在文本字段中输入的值。)
  4. 提交表单时,它没有正确保存/使用文本字段中输入的值,我得到的答案有时错了。 (可能与#2相同。)
  5. modulo运算符有时会生成" 0"打赌。显然,你不能赌赌场游戏,仍然会赢。下注金额需要为5到100,增量为5.(5,10,55,80等)
  6. 这是我的代码。我仍处于PHP的学习阶段,因此感谢任何和所有信息。我甚至不能完全确定我做对了。 (具有讽刺意味的是,如果这是C#,我就不会在这里......)

    <?php
    // Defaults
    $dealer_answer = $_POST['dealer_answer'];
    
    // Generate random bet
    $random_bet = rand(5,100);
    $random_bet = $random_bet % 5;
    $random_bet = $random_bet *5;
    
    // Generate roll
    $roll = 10;
    $factor = 1.8;
    
    // Place bet
    $winning_bet = $factor*$random_bet;
    $winning_bet = floor($winning_bet);
    echo $winning_bet; // Display current value for testing purposes
    echo '<p>' . $dealer_answer . '</p>'; // Display current value for testing purposes
    ?>
    
    <p>Player has a bet of <?php echo $random_bet; ?> on a Place Bet 10.</p>
    <p>Player rolls a <?php echo $roll; ?>. How much does the player win?
    <form method="POST" action="">
        <input type="text" name="dealer_answer" value="<?php $dealer_answer; ?>">
        <input type="hidden" name="winning_bet" value="<?php $winning_bet; ?>">
        <input type="submit" value="Submit" name="submit">
    </form>
    
    <?php
    session_start();
    // Check if answer is right
    if (isset($_POST['submit']))
        if ($dealer_answer == $winning_bet) {   
            echo "That's right!";
        } else {
            echo "That is incorrect.";
    }
    

1 个答案:

答案 0 :(得分:1)

尝试将代码更改为:

<?php
    // Defaults
    //Generate random bet
    $random_bet = rand(5,100);
    $random_bet = $random_bet % 5;
    $random_bet = $random_bet *5;

    // Generate roll
    $roll = 10;
    $factor = 1.8;        

    // Place bet
    $winning_bet = $factor*$random_bet;
    $winning_bet = floor($winning_bet); 

    echo "Winning bet is = ".$winning_bet; // Display current value for testing purposes

    if ( isset( $_POST ) && isset( $_POST['dealer_answer'] ) ) {
        var_dump($_POST);
        $dealer_answer = $_POST['dealer_answer'];
        $winning_bet = $_POST['winning_bet'];

        var_dump("Dealer answer = ".$dealer_answer);
        var_dump("Winning bet = ".$winning_bet);

        if ($dealer_answer == $winning_bet) {   
                echo "That's right!";
        } else {
            echo "That is incorrect.";
        }        



        echo '<p>' . $dealer_answer . '</p>'; // Display current value for testing purposes
    }
?>

<p>Player has a bet of <?php echo $random_bet; ?> on a Place Bet 10.</p>
<p>Player rolls a <?php echo $roll; ?>. How much does the player win?
<form method="POST" action="">
    <input type="text" name="dealer_answer" value="<?php if (isset($_POST['dealer_answer'])) { echo $dealer_answer; } ?>">
    <input type="hidden" name="winning_bet" value="<?php echo $winning_bet; ?>">
    <input type="submit" value="Submit" name="submit">
</form>

我已将var_dump留在那里,以便您可以看到变量。