0

Right now I'm working on a PHP project where the user enters the lower limit and upper limit and the program will generate 5 numbers between that limit, print it out the randomly generated 5 numbers, and find and print the sum.

For some reason my program isn't getting passed the input (lower and upper limits).

<!DOCTYPE html>
<html lang = "en">
   <head>
      <title>Sum of the Digits!</title>
   </head>
   <body>
      <h1>Find the sum of five digits!</h1>
      <p>Enter in the lower and upper limits of the numbers you would like to 
         generate. Press "Calculate" to calculate the sum of the genreated numbers!
      </p>
      <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
         Enter the lower limit: <input type = "number" name = "lowLim">
         Enter the upper limit: <input type = "number" name = "upLim">
         <input type = "submit">
      </form>
      <?php
         if($SERVER["REQUEST_METHOD"] == "POST"){

         $lowerLim = test_input($_POST["lowLim"]);
         $upperLim = test_input($_POST["upLim"]);
         $randomArray = array();
         $sumArray = array();
         $total = 0;
         $arrCounter = 0;
         var_dump ($lowerLim);



         for($i = 0; $i < 5; $i++){
         $randomRange = rand("$lowerLim","$upperLim");
         $randomArray = array($randomRange[i]);
         }

         $sumArray = array($randomArray);
         $total = array_sum($sumArray);
         }
         function test_input($data){
         $data = trim($data);
         $data = stripslashes($data);
         $data = htmlspecialchars($data);
         return $data;
         }




         echo "First number generated: ".$randomArray[0];
         echo "Second number generated: ".$randomArray[1];
         echo "Third number generated: ".$randomArray[2];
         echo "Fourth number generated: ".$randomArray[3];
         echo "Fifth number generated: ".$randomArray[4];



         echo "The sum of the generated digits is:".$total;


         ?>
   </body>
</html>
Erik Kalkoken
  • 23,798
  • 6
  • 53
  • 81
Josh Smith
  • 19
  • 1
  • 10

1 Answers1

0

You had a couple of issues in your code:

  • typo: $_SERVER, not $SERVER
  • The result output need to be inside the if ... == POST brackets, or they will be called with the input
  • In your loop of assigning the random values, you kept overwriting the $randomArray

  • array_sum() needs to be called on the $randomArray directly, not on an array of the random array.

Here is a corrected version of your code:

<!DOCTYPE html>
<html lang = "en">
   <head>
      <title>Sum of the Digits!</title>
   </head>
   <body>
      <h1>Find the sum of five digits!</h1>
      <p>Enter in the lower and upper limits of the numbers you would like to 
         generate. Press "Calculate" to calculate the sum of the genreated numbers!
      </p>
      <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
         Enter the lower limit: <input type = "number" name = "lowLim">
         Enter the upper limit: <input type = "number" name = "upLim">
         <input type = "submit">
      </form>
      <?php
         if($_SERVER["REQUEST_METHOD"] == "POST")
         {

             $lowerLim = test_input($_POST["lowLim"]);
             $upperLim = test_input($_POST["upLim"]);
             $randomArray = array();
             $sumArray = array();
             $total = 0;
             $arrCounter = 0;
             var_dump ($lowerLim);



             for($i = 0; $i < 5; $i++)
             {
                 $randomRange = rand("$lowerLim","$upperLim");
                 $randomArray[] = $randomRange;
             }

             $total = array_sum($randomArray);

              echo "First number generated: " . $randomArray[0];
             echo "Second number generated: " . $randomArray[1];
             echo "Third number generated: " . $randomArray[2];
             echo "Fourth number generated: " . $randomArray[3];
             echo "Fifth number generated: " . $randomArray[4];
               echo "The sum of the generated digits is:".$total;
         }

         function test_input($data)
         {
             $data = trim($data);
             $data = stripslashes($data);
             $data = htmlspecialchars($data);
             return $data;
         }

         ?>
   </body>
</html>
Erik Kalkoken
  • 23,798
  • 6
  • 53
  • 81