Jump to content

Keeping randomly generated numbers and checking the sum


cenalt

Recommended Posts

Hey,

This is my first day with PHP and I'm given an assignment by my professor. Assignment is a very basic game, all i have to do is give 2 random numbers, and add them, and check if the user has submitted the correct answer. 

 

So i thought like this;

 

1-    first php file generates two random numbers

1.1- user submits the sum of those numbers

 

2-    second php file, gets the sum of the random generated numbers and checks if the submitted value is right.

 

I've come up this far;

 

<?php
echo "<html><head><title>calculation game!</title></head><body>";
echo "<h1>first number is</h1>";
echo (rand(100, 450));
echo "<h1>and the next number is</h1>";
echo (rand(100, 5000));
echo "<h1>now add the numbers and submit your output here!</h1>";
echo "<form name = \"myfirstform\" action  = \"formprocess.php\" method = \"PO$
echo "Enter data<br>";
echo "<input type = \"text\" name = \"firstdata\">";
echo "<br> <input type= \"submit\" value = \"Let's check!\">";
echo "</form>";

?>   

 

Now my problem is about; how can I keep those generated numbers and add them up. I've been digging all through the internet for syntax and php scripts, but I couldn't find an answer that helps me. 

 

Thanks in advance.

First, store the random numbers that you generate

echo "<html><head><title>calculation game!</title></head><body>";
$n1 = rand(100, 450);
$n2 = rand(100, 5000);

echo "<h1>first number is</h1>";
echo $n1;
echo "<h1>and the next number is</h1>";
echo $n2;

Then put those values in hidden form fields so they get sent with the sum

echo "<form name = \"myfirstform\" action  = \"formprocess.php\" method = \"POST\">
echo "Enter data<br>";
echo "<input type = \"text\" name = \"firstdata\">";
echo "<input type='hidden' name='n1' value='$n1'>";                                 // hidden form field
echo "<input type='hidden' name='n2' value='$n2'>";;                                // hidden form field
echo "<br> <input type= \"submit\" value = \"Let's check!\">";
echo "</form>";

Thank you very much for fast reply! I've understood the logic behind it finally but the script returns as an empty page, and i've been checking but can't figure out why. What's did I do wrong ?

<?php
echo "<html><head><title>calculation game!</title></head><body>";
$n1 = rand(100, 450);
$n2 = rand(100, 5000);

echo "<h1>first number is</h1>";
echo $n1;
echo "<h1>and the next number is</h1>";
echo $n2;
echo "<form name = \"myfirstform\" action  = \"formprocess.php\" method = \"POST\">;
echo "Enter sum";
echo "<input type = \"text\" name = \"firstdata\">";
echo "<input type='hidden' name='n1' value='$n1'>";
echo "<input type='hidden' name='n2' value='$n2'>";
echo "<input type= \"submit\" value = \"Let's check!\">";
echo "</form>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.