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>
Requestparameter change afterSubmit. AfterSubmit,$anshave no value. You have to changeactionparameter or choose an other way to maintain$ansvalue.