cmdshorty
cmdshorty

Reputation: 35

PHP Form - Comparing a Set Value with User Inputted Value

I'm in the early stages of creating a training tool for casino dealers. I have my code working at about 75%.

The tool is pretty simple: code generates a random bet amount between 5 and 100, generates a random dice roll number between 2 and 12, calculates the $winning_bet amount based on the bet and $factor value, user inputs answer in a text field, code lets you know if it's right or wrong with an if-else statement.

Here are the issues I'm having:

  1. When I first load the file in my browser, I get an "undeclared value" error for $dealer_answer
  2. Even when echoing the right answer (to ensure I know my math is right) and inputting that right answer in the text field, I still get a "That is incorrect." response. (NOTE: I assume this is because it's using the LAST saved value and not the value currently entered in the text field by the user.)
  3. When submitting the form, it's not properly saving/using the entered value in the text field and I get the answer wrong some times. (Might be the same issue as #2.)
  4. The modulo operator is, at times, generating a "0" bet. Obviously you can't bet on a casino game and still win. The bet amount needs to be 5 to 100 in increments of 5. (5, 10, 55, 80, etc.)

Here is my code. I'm still in the learning stages of PHP so any and all information is appreciated. I'm not even entirely sure I'm doing this right. (Ironically, if this were C#, I wouldn't be here...)

<?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.";
}

Upvotes: 0

Views: 49

Answers (1)

Nick Duncan
Nick Duncan

Reputation: 829

Try changing your code to :

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

I've left the var_dumps in there so you can see the variables along the way.

Upvotes: 1

Related Questions