Jump to content

Recommended Posts

This is my first PHP script, and it’s working, except not as I planned.

 

When I submit an answer the rand variables change making my answer incorrect.

 

How do I correct this and still have a new equation to answer?

 

Here is my code: 

 

<?php

 

$x = rand (2, 12);

$y = rand (10, 20);

$num = $x + $y;

 

print ("$x + $y = ");

# print ("$num");

 

$message = "";

 

if ( ! isset ($answer) )

$message = "Welcome to my math machine!";

elseif ($answer > $num)

$message = "Your answer is too high. Try again.";

elseif ($answer < $num)

$message = "You answered is too low. You can do this!.";

else // correct

$message = "Well Done! - Correct Answer -";

?>

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Randon Number Equation</title>

</head>

 

<body>

 

<h4>

<?php print $message ?>

</h4>

 

<form method="post">

Type your answer here: <input type="text" name="answer" />

<input name="submit" type="submit" value="SUBMIT">

</form>

 

</body>

</html>

 

Thanks, Dan

Link to comment
https://forums.phpfreaks.com/topic/176308-solved-a-simple-problem/
Share on other sites

well first of all, you aren't even getting the post variable. But you should send the answer through a hidden field. like do something similar to this

<html>
<body>
<?php
if (isset($_POST['answer'])){
$answer = $_POST['answer'];//$_POST['answer'] is how you access post variables. its $_POST['input name']
$oldNum = $_POST['oldnum'];
echo $answer . " == " . $oldNum."<br />";
}
$x = rand (2, 12);
$y = rand (10, 20);
$num = $x + $y;

print ("$x + $y = ");
# print ("$num");

$message = "";

if ( ! isset ($answer) )
   $message = "Welcome to my math machine!";
elseif ($answer > $oldNum)
   $message = "Your answer is too high. Try again.";
elseif ($answer < $oldNum)
   $message = "You answered is too low. You can do this!.";
else // correct
   $message = "Well Done! - Correct Answer -";
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Randon Number Equation</title>
</head>

<body>

<h4>
<?php print $message ?>
</h4>

<form method="post">
Type your answer here: <input type="text" name="answer" />
<input type="hidden" name="oldnum" value="<?php echo $num; ?>" />
<input name="submit" type="submit" value="SUBMIT">
</form>

</body>
</html>

 

try that. Tested and works for me

 

PFMaBiSmAd : No concerns about cheating. This is almost week two on learning PHP. I thought it would be an easy practice to create an equation script.  Apparently it is, but not for me, yet.

 

Mikesta707: Thanks for the repair/finish to my code.  I'll have to read it a few more times to fully understand how it works.

 

Dan

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.