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.

Link to comment
Share on other sites

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>";
Edited by Barand
  • Like 1
Link to comment
Share on other sites

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>";
?>
Link to comment
Share on other sites

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.