tags:

views:

145

answers:

4

Ok I am trying to make something to ask you random multiplication questions. Now it asks the questions fine. Generates the random questions fine. But when it reloads the page the random numbers are different...

how can I fix this?

<?php 

$rndnum1 = rand(1, 12); 
$rndnum2 = rand(1, 12);

echo "<h3>". $rndnum1 . " x ";
echo $rndnum2 . "</h3>";

if($_SERVER["REQUEST_METHOD"] == "GET") {

 $answer=0;

}
else if($_SERVER["REQUEST_METHOD"] == "POST") {

 $answer=trim($_POST["answerInput"]);
 $check=$rndnum1*$rndnum2;

 if($answer==$check) {

  echo "Correct!";

 }
 else {

  echo "Wrong!";

 }


}

?>

<form action="<?php echo $_SERVER['PHP_SELF'];  ?>" method="post" >
 <table>
  <tr>
   <td>
    First Name:&nbsp;
   </td>
   <td>
    <input type="text" name="answerInput" value="<?php echo $answer; ?>" size="20"/>
   </td>
   <td>
    <?php echo $answerError; ?>
   </td>
  </tr>
  <tr>
   <td class="signupTd" colspan="2">
    <input type="submit" name="submit" value="Submit"/>
   </td>
  </tr>
 </table>
</form>
A: 

add srand(0) to top of your code.

actually to you may want to use a cookie that is randomly initialised, and then pass that to srand()

steelbytes
I don't think he's trying to make the random numbers the same every time the page loads. I think he's trying to compare the multiplication of the two random numbers from the previous load of the page to the answer posted at the current load of the page. I could be wrong...
Ben Torell
that doesnt work at all =[
MrEnder
I'm trying to have it ask a new question each time. so ya
MrEnder
ok, think of this - how do you know the difference between a refresh, and a new question? that is why I suggest a cookie
steelbytes
+2  A: 

When you reload the page, $rndnum1 and $rndnum2 are set to new random numbers via the rand() function. That's why they are not staying the same. Try passing the original random numbers along with the POST, and calculate the numbers from $_POST before checking if the answer's correct.

To do this, make sure you include the following line for both random variables in your submission form:

<input type="hidden" name="rndnum1" value="<?php echo $rndnum1 ?>" />

Then, on the next load of the page after the answer form is submitted, get the numbers with $_POST['rndnum1'], etc.

Ben Torell
Ya problem is I have no idea how to make that work... I'm attempting to do that at this very moment and can't figure it out =[
MrEnder
How are you submitting the answer? I'm assuming via a form? If that's the case, add `<input type="hidden" name="rndnum1" value="<?php echo $rndnum1 ?>" />` for both the random numbers to the form, and then get those numbers with `$_POST['rndnum1']`, etc. when you want to compare the answer.
Ben Torell
thanks that worked ^.^
MrEnder
+1  A: 

Include the generated random numbers in hidden form fields so they're submitted to the server.

For example, just inside <form>:

<input type="hidden" name="rand1" value="<?=$rndnum1?>">
<input type="hidden" name="rand2" value="<?=$rndnum2?>">

Then in PHP, when you're processing the form, use $_POST['rand1'] and $_POST['rand2'] to retreive the original numbers, multiply, then compare with the user's given answer.

Jason Cohen
thanks that worked ^.^
MrEnder
A: 

Something like this should work.

<?php 
@session_start();

if($_SERVER["REQUEST_METHOD"] == "GET")
{
    $answer=0;
}

if (!$_POST['submit'])
{
    $_SESSION['rndnum1'] = rand(1, 12);
    $_SESSION['rndnum2'] = rand(1, 12);
}
else
if($_SERVER["REQUEST_METHOD"] == "POST")
{
    $answer = trim($_POST["answerInput"]);
    $check = $_SESSION['rndnum1']*$_SESSION['rndnum2'];
    if( $answer == $check)
    {
        $msg = "Correct!";
    }
    else
    {
        $msg = "Wrong!";
    }
}
echo "<h3>". $_SESSION['rndnum1'] . " x " . $_SESSION['rndnum2'] . "</h3>";
if ($_POST['submit'])
{
    echo $msg;
}





?>

<form action="<?php echo $_SERVER['PHP_SELF'];  ?>" method="post" >
 <table>
  <tr>
   <td>
    First Name:&nbsp;
   </td>
   <td>
    <input type="text" name="answerInput" value="<?php echo $answer; ?>" size="20"/>
   </td>
   <td>
    <?php echo $answerError; ?>
   </td>
  </tr>
  <tr>
   <td class="signupTd" colspan="2">
    <input type="submit" name="submit" value="Submit"/>
   </td>
  </tr>
 </table>
</form>
Adrian A.
Store everything in sessions, in case it's a POST, check the result, if not, regenerate numbers.
Adrian A.