cenalt Posted November 29, 2014 Share Posted November 29, 2014 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; <?phpecho "<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. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 29, 2014 Share Posted November 29, 2014 (edited) 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 November 29, 2014 by Barand 1 Quote Link to comment Share on other sites More sharing options...
cenalt Posted November 29, 2014 Author Share Posted November 29, 2014 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>"; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 29, 2014 Share Posted November 29, 2014 Sorry, my fault. Missing " at the end of echo "<form name = \"myfirstform\" action = \"formprocess.php\" method = \"POST\">"; Quote Link to comment Share on other sites More sharing options...
cenalt Posted November 29, 2014 Author Share Posted November 29, 2014 Thank you very much! It works perfectly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.