Home > Software engineering >  PHP: Generating a html table through a form
PHP: Generating a html table through a form

Time:09-27

This is practice coursework for my Informatics course. We've just started programming in PHP and our task is to program a website that generates tables for the user.

  1. password prompt
  2. ask the user how many rows and columns the table should have
  3. based on the last question; create a form with the same amount of input boxes
  4. generate the table with the input of step 3

I've accomplished everything until step 4. The user can input data in the form, but I the problem is that when I try to generate the table, PHP will show me this error message: "Undefined index: rows on line 70".

As I described earlier I'm just about to learn PHP, so there may be many "not so very nice programming approaches"; therefore I'm open to all kinds of recommendations.

<!DOCTYPE html>
<html>
    <body>
        <form method="post" target="">
            <label for="login">User: </label>
            <input name="login">
            <br />
            <label for="password">Password: </label>
            <input name="password" type="password">
            <br />
            <input type="submit" name="generate" value="Login" />
        </form>

        <?php
            if (isset($_POST['generate'])) {
                $username = $_POST['login'];
                $password = $_POST['password'];
                $hashed_username = sha1($username);
                $hashed_password = sha1($password);
                $correct_username = '9d6035e25958ec12fca7ec76d68c8daaf4815b9b'; //wims
                $correct_password = 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'; //test

                if ($hashed_username != $correct_username)
                    die("Wrong user name!");
                if ($hashed_password != $correct_password)
                    die("Wrong password!");

                echo "How many rows and columns should the table have? <br />";
                echo('
                    <form method="POST" target="">
                        Rows: <input type="number" name="rows" min="1" max="100"/><br/>
                        columns: <input type="number" name="columns" min="2" max="100"/><br/>
                        <input type="submit" name="generate1" value="Generate Table" />
                    </form>');
            }

            if (isset($_POST['generate1'])) {
                $rows = $_POST['rows'];
                $columns = $_POST['columns'];
                global $rows, $columns;
                if ($rows > 100 || $rows < 1) 
                    die("Nope!");
                if ($columns > 100 || $columns < 2)
                    die("Nope!");

                echo '<form method="POST" target="">';
                echo "<table>";
                for ($a=1;$a<=$rows;$a  ) {
                    echo "<tr>";
                    for ($b=0;$b<=$columns;$b  ) {
                        if ($b==0)
                            echo "<td>$a. Row</td>";
                        else {
                            $c = $a . $b;
                            echo "<td><input type='text' name='$c' /></td>";
                            }
                            
                    }
                    echo "</tr>";
                }
                
                echo "</table>";
                echo "<input type='submit' name='generate2' value='Generate' />";
                echo "</form>";
            }
            

            if (isset($_POST['generate2'])) {
                echo "<table>";
                for ($a=1;$a<=$GLOBALS['rows'];$a  ) {
                    echo "<tr>";
                    for ($b=0;$b<=$GLOBALS['columns'];$b  ) {
                        if ($b==0)
                            echo "<td>$a. row</td>";
                        else {
                            $c = $a . $b;
                            echo "<td>$_POST[$c]</td>";  
                    }
                    echo "</tr>";
                }
                echo "</table>";
                }
            }
        ?>
    </body>
</html>

CodePudding user response:

Your code here is the problem

        if (!isset($_POST['generate1']))
            die('');

Here you are checking if $_POST['generate1] is set, if it is not then die (halt/terminate execution of the script)

php die();

So when you submit your second form (submit has name of generate2) then the above check will fail (it is not set so it will die(); and end execution of your script.

if (isset($_POST['generate1'])) {
    // Show the form....
}

Do this for both the generate1 and generate2 and it will only execute that code if the if statements evaluates to true.

CodePudding user response:

instead using "if (!isset($_POST['generate1']))" change it into "if(isset($_POST['generate1']))".When you click generate2 it will be die because that condition is not fulfilled.

<!DOCTYPE html>
<html>
    <body>
        <form method="post" target="">
            <label for="login">User: </label>
            <input name="login">
            <br />
            <label for="password">Password: </label>
            <input name="password" type="password">
            <br />
            <input type="submit" name="generate" value="Login" />
        </form>

        <?php
            if (isset($_POST['generate'])) {
                $username = $_POST['login'];
                $password = $_POST['password'];
                $hashed_username = sha1($username);
                $hashed_password = sha1($password);
                $correct_username = '9d6035e25958ec12fca7ec76d68c8daaf4815b9b'; //wims
                $correct_password = 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'; //test

                if ($hashed_username != $correct_username)
                    die("Wrong user name!");
                if ($hashed_password != $correct_password)
                    die("Wrong password!");

                echo "How many rows and columns should the table have? <br />";
                echo('
                    <form method="POST" target="">
                        Rows: <input type="number" name="rows" min="1" max="100"/><br/>
                        columns: <input type="number" name="columns" min="2" max="100"/><br/>
                        <input type="submit" name="generate1" value="Generate Table" />
                    </form>');
            }

            if (isset($_POST['generate1'])){
                
              $rows = $_POST['rows'];
              $columns = $_POST['columns'];
              
              if ($rows > 100 || $rows < 1) 
                  die("Nope!");
              if ($columns > 100 || $columns < 2)
                  die("Nope!");

              echo "<form method='POST' target=''>";
              echo "<input type='hidden' name='row' value='$rows'/>";
              echo "<input type='hidden' name='column' value='$columns'/>";
              echo "<table>";
              for ($a=1;$a<=$rows;$a  ) {
                  echo "<tr>";
                  for ($b=0;$b<=$columns;$b  ) {
                      if ($b==0)
                          echo "<td>$a. Row</td>";
                      else {
                          $c = $a . $b;
                          echo "<td><input type='text' name='$c' /></td>";
                          }
                          
                  }
                  echo "</tr>";
              }
              
              echo "</table>";
              echo "<input type='submit' name='a' value='Generate' />";
              echo "</form>";
            
          }

          if (isset($_POST['a'])) {
            $rows = $_POST['row'];
            $columns = $_POST['column'];
            echo "<table border='1'>";
            for ($a=1;$a<=$rows;$a  ) {
                echo "<tr>";
                for ($b=0;$b<=$columns;$b  ) {
                    if ($b==0){
                        echo "<td>$a. row</td>";
                    }else {
                        $c = $a . $b;
                        echo "<td>$_POST[$c]</td>";  
                    }
                }
                echo "</tr>";
            }
            echo "</table>";
          }
        ?>
    </body>
</html>

CodePudding user response:

You need to put your $rows and $columns variables into $_SESSION values. With $Globals, I assume you cannot reach to that point. Because they are not declared the second time you reload the page by submitting the second form.

In fact, as W3Schools states, "$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script", while a "session is a way to store information (in variables) to be used across multiple pages." When you submit the pages for the second time, you are in fact refreshing the page, and here Globals are not a pick for you access your rows and columns. Instead you should use sessions to store your $_POST['rows'] and $_POST['columns'].

So, try the following instead. Start Session and then declare new $_Session variables for your $_POST['rows'] and $_POST['columns']. Then voila, the problem is solved.

if (isset($_POST['generate'])) {
    $username = $_POST['login'];
    $password = $_POST['password'];
    $hashed_username = sha1($username);
    $hashed_password = sha1($password);
    $correct_username = '9d6035e25958ec12fca7ec76d68c8daaf4815b9b'; //wims
    $correct_password = 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'; //test

    if ($hashed_username != $correct_username)
        die("Wrong user name!");
    if ($hashed_password != $correct_password)
        die("Wrong password!");

    echo "How many rows and columns should the table have? <br />";
    echo('
                    <form method="POST" target="">
                        Rows: <input type="number" name="rows" min="1" max="100"/><br/>
                        columns: <input type="number" name="columns" min="2" max="100"/><br/>
                        <input type="submit" name="generate1" value="Generate Table" />
                    </form>');
}

if (isset($_POST['generate1'])) {

    $rows = $_POST['rows'];
    $columns = $_POST['columns'];
    $_SESSION['rows'] = $_POST['rows'];
    $_SESSION['columns'] = $_POST['columns'];

    global $rows, $columns;
    if ($rows > 100 || $rows < 1)
        die("Nope!");
    if ($columns > 100 || $columns < 2)
        die("Nope!");

    echo '<form method="POST" target="">';
    echo "<table>";
    for ($a = 1; $a <= $rows; $a  ) {
        echo "<tr>";
        for ($b = 0; $b <= $columns; $b  ) {
            if ($b == 0)
                echo "<td>$a. Row</td>";
            else {
                $c = $a . $b;
                echo "<td><input type='text' name='$c' /></td>";
            }

        }
        echo "</tr>";
    }

    echo "</table>";
    echo "<input type='submit' name='generate2' value='Generate' />";
    echo "</form>";

}

if (isset($_POST['generate2'])) {
    echo "<table>";
    $row = $_SESSION['rows'] ?? '';
    $columns = $_SESSION['columns'] ?? '';
    for ($a = 1; $a <= $row; $a  ) {
        echo "<tr>";
        for ($b = 0; $b <= $columns; $b  ) {
            if ($b == 0)
                echo "<td>$a. row</td>";
            else {
                $c = $a . $b;
                echo "<td>$_POST[$c]</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    }
    session_destroy();

}

CodePudding user response:

First

Your primary need is to READ THE MANUAL for all the things you're doing, thus you will see that the sha1() Manaul page states:

Warning

It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm.

You REALLY should be fixing this issue.


Anyway, your issue is:

Undefined index: rows on line 70

Which (I guess, because you didn't indicate in your question) is this line:

for ($a=1;$a<=$GLOBALS['rows'];$a  ) {

This means that $GLOBALS key rows doesn't exist. Why? All PHP data is generated when a script is executed; before ANY script starts, the PHP knows NOTHING, there is never any incoming data at the start if the PHP script.

Some people here might shout and scream "SESSIONS!!" but even the $_SESSION array is empty at the start of the script, until the PHP code has read the stored session data in the cookie key.

So how do you populate $GLOBALS? What you did was not far off, but you ran the form and submitted the data to $_POST['generate1'] which worked, and this populated the data, but this presented a form to the end user so that user then had to resubmit the form, and by default that reloads the page, therefore restarting the PHP script from zero again, so all data in $GLOBALS is forgotten.

How do you make PHP "remember" data when loading a page? In general there are several ways; all of them have positive and negative sides:

  1. Database. Read and write data to a third party
  2. Sessions. Read and write data to a file/database associated with that specific client only.
  3. Form data, reading data from a submitted form or via URL parameters (GET/POST).

Using (3) is probably easiest for you; so when you run the $_POST['generate1'] you need to add hidden inputs to your form so your "part 2" form can then pass on this data to "part3" form ($_POST['generate2']) .

And that's the data you need to read, not the GLOBALS.

  • Related