0

So, I'm building a simple program that requires the user input a number which is then posted and generated into several random similar numbers.

The program then requires the user choose the correct variable, which would then be validated by the program.

I'm having issues with what I think is a variable not being 'available' for lack of a better word in the if/else statement.

I feel like I'm making a really simple/stupid mistake.

<?php
$numb = $_GET["number"];
switch ($numb) {
    case 1:
        echo "1x<br>";
        $ans = 1; $n1 = rand(($ans - 5), ($ans + 5)); $n2 = rand(($ans - 5), ($ans + 5)); $n3 = 1; $n4 = rand(($ans - 5), ($ans + 5));
    break;
    case 2:
        echo "2";
    break;
    case 3:
        echo "3";
    break;
} echo $ans;

if(isset($_POST['submit']))
{
    $rb = $_POST['radio'];
    if($rb == $ans){echo "test";}
    else{echo "fail";}
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="radio" name="radio" value="<?php echo $n1; ?>"><?php echo $n1 ?>
<input type="radio" name="radio" value="<?php echo $n2; ?>"><?php echo $n2 ?>
<input type="radio" name="radio" value="<?php echo $n3; ?>"><?php echo $n3 ?>
<input type="radio" name="radio" value="<?php echo $n4; ?>"><?php echo $n4 ?>
<input type="submit" name="submit" value="submit" />
</form>
1
  • You always get "fail"? Request parameter change after Submit. After Submit, $ans have no value. You have to change action parameter or choose an other way to maintain $ans value. Commented Sep 29, 2017 at 2:41

2 Answers 2

1

You have to define the variables before useing them in the switch/case.

// Check if get is used
if (isset($_GET['number'])){

$numb = $_GET["number"];
// predefine Variables here
$ans = '';
$n1 = '';
$n2 = '';
$n3 = '';
$n4 = '';
switch ($numb)
{
    case 1:
        echo "1x<br>";
        $ans = 1;
        $n1  = rand(($ans - 5), ($ans + 5));
        $n2  = rand(($ans - 5), ($ans + 5));
        $n3  = 1;
        $n4  = rand(($ans - 5), ($ans + 5));
        break;
    case 2:
        echo "2";
        break;
    case 3:
        echo "3";
        break;
}
echo $ans;

if (isset($_POST['submit']))
{
    $rb = $_POST['radio'];
    if ($rb == $ans)
    {
        echo "test";
    }
    else
    {
        echo "fail";
    }
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="radio" name="radio" value="<?php echo $n1; ?>"><?php echo $n1 ?>
    <input type="radio" name="radio" value="<?php echo $n2; ?>"><?php echo $n2 ?>
    <input type="radio" name="radio" value="<?php echo $n3; ?>"><?php echo $n3 ?>
    <input type="radio" name="radio" value="<?php echo $n4; ?>"><?php echo $n4 ?>
    <input type="submit" name="submit" value="submit"/>
</form>
<?php
} else {
    echo "Number not set in get!";
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have to use $global variable.

PHP Variables Manual

<?php
$numb = $_GET["number"];
$ans = 0;
switch ($numb) {
    case 1:
        echo "1x<br>";
        $ans = 1;
        $n1 = rand(($ans - 5), ($ans + 5));
        $n2 = rand(($ans - 5), ($ans + 5));
        $n3 = 1;
        $n4 = rand(($ans - 5), ($ans + 5));
    break;
    case 2:
        echo "2";
    break;
    case 3:
        echo "3";
    break;
} echo 'Answer : [' . $ans . ']<br>';

if(isset($_POST['submit']))
{
    $rb = $_POST['radio'];
    echo 'PostAnswer : [' . $ans . ']<br>';
    if($rb == $ans)
    {
        echo "Good!";
    }
    else
    {
        echo "Bad!";
    }
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>?number=1" method="POST">
<input type="radio" name="radio" value="<?php echo $n1 ?>"><?php echo $n1 ?>
<input type="radio" name="radio" value="<?php echo $n2 ?>"><?php echo $n2 ?>
<input type="radio" name="radio" value="<?php echo $n3 ?>"><?php echo $n3 ?>
<input type="radio" name="radio" value="<?php echo $n4 ?>"><?php echo $n4 ?>
<input type="submit" name="submit" value="submit" />
</form>

Your page will change after pressing submit button.

ex:) url_php = test.php request parameter : ?number=1

1) inputing /test.php?number=1

2) $ans = 1

3) After pressing submit button

4) $ans = ""

Like that! You have to maintain ?number=1 or fix it!

2 Comments

I started building the program out to include more switch cases and it will only work with case/number 1. The program is the same besides adding the code from switch 1 to switch 2-10. The program will as I said work with case 1 but when you try 2-10 every choice will be incorrect.
Now, I cannot clearly understand what you said. I think you cannot get posted data using $_GET. What does "user input a number" mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.