0

I am making a script which is going to add together a bunch of inputs. I am almost there, but when testing the script i seem to get the alert: Notice: Undefined index: number in C:\xampp\htdocs\Archers.php on line 25. Even though I have declared it earlier and even when using it now. The script is below. Thanks in advance.

<html>
<head>
</head>
<body>
  <form method="post">
    Enter how many values you would like to enter:
    <input type="number" name="number">
    <input type="submit" name="submit">
  </form>
  <?php
  $number = 0;
  $result = 0;
  if (isset($_POST["submit"])){
    $number = $_POST['number'];
    $x = 0;
    echo "<form method=\"post\">";
    while ($x != $number) {
      echo "Enter score: <input type=\"text\" name=\"".'a'.$x."\"><br>";
      $x = $x + 1;
    }
    echo "<input type=\"submit\" name=\"submit2\"></form>";
  }
  if (isset($_POST["submit2"])){
    $y = 0;
    $number = $_POST['number'];
    while ($y != $number){
      $value = $_POST["a".$y];
      $result = $result + $value;
      $y = $y + 1;
    }
    echo $result;
  }
  ?>
</body>
</html>

2 Answers2

0

The error you received is because the index of array is undefined - it simply does not exist. That's probably $number = $_POST['number'] line in your code (25th).

It surely means that you did not fill number input in your form, as you have no validation to a possibility that number field is not passed.

EDIT: I can see that you have created second form, which has submit2 key in it. When you POST for the first time, you will receive both submit and number keys (in lines 14 - 22). Then your code creates second form, which makes another POST with submit2 but without number field in it. Therefore, you don't have number key in lines 23 - 32.

EDIT2: You can, for example, change the first if to be like this one:

if (isset($_POST["submit"])){
    $number = $_POST['number'];
    $x = 0;
    echo "<form method=\"post\">";
    while ($x != $number) {
        echo "Enter score: <input type=\"text\" name=\"".'a'.$x."\"><br>";
        $x = $x + 1;
    }
    echo "<input type=\"hidden\" name=\"number\" value=\"" . $number . "\">";
    echo "<input type=\"submit\" name=\"submit2\"></form>";
}
Tafit
  • 1
  • 2
0

Please try this code . it will remove your error

 <html>
    <head>
    </head>
    <body>
      <form method="post">
        Enter how many values you would like to enter:
        <input type="number" name="number">
        <input type="submit" name="submit">
      </form>
      <?php
      $number = 0;
      $result = 0;
      if (isset($_POST["submit"]) && isset( $_POST['number']) ){
        $number = $_POST['number'];
        $x = 0;
        echo "<form method=\"post\">";
        while ($x != $number) {
          echo "Enter score: <input type=\"text\" name=\"".'a'.$x."\"><br>";
          $x = $x + 1;
        }
        echo "<input type=\"submit\" name=\"submit2\"></form>";
      }
      if (isset($_POST["submit2"]) && isset( $_POST['number'])){
        $y = 0;

        $number = $_POST['number'];

        while ($y != $number){
          $value = $_POST["a".$y];
          $result = $result + $value;
          $y = $y + 1;

        echo $result;
          }
      }
      ?>
    </body>
    </html>
lakshman rajput
  • 511
  • 5
  • 14