skip to Main Content

I need to create a table of 17 rows where each row contains information such as row number, name, surname, email and birthday. The data is provided by this form:

<form action="index.php" method="post">
      <input type="text" name="name" placeholder="name" />
      <input type="text" name="surname" placeholder="surname" />
      <input type="text" name="emailbirthday" placeholder="emailbirthday" />
      <input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
      <button type="reset">Reset Form</button>
      <button type="submit">Submit Form</button>
    </form>

After clicking submit the data should be displayed in the nth row of the table(row number one if it is the first "pack" of data submitted, number two if its the second and so on). This problem could easely be solved using databases but i cannot use them(by professors order).

I tried to create an array than push values into it like this:

$array_name = array();

$name = $_POST["name"];

array_push($array_name, $name);

This approach doesn’t work(the index of the array stays 0 alla of the time so it keeps replacing the first value again and again) and manually incrementing the index counter of the array doesn’t work either.

3

Answers


  1. Normally one should use a database approach but your professor explicitly forbids it.

    There are many other ways to do it. (store as TEXT/JSON/CSV file or localstorage / cookies), etc. For me I would use session to do the job

    • declare a session variable which is an array
    • if user submits the form, store the POST data into another array (subarray) containing name, surname, birthday, email
    • add the subarray into the main session variable array
    • print it out at the end as a table

    So the PHP will be:

    <?php
    session_start();
    ?>
    <form action="#" method="post">
          <input type="text" name="name" placeholder="name" />
          <input type="text" name="surname" placeholder="surname" />
          <input type="text" name="email" placeholder="email" />
          <input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
          <button type="reset">Reset Form</button>
          <button type="submit">Submit Form</button>
    </form>
    
    <?php
    
    if (!isset($_SESSION["arr"])){
      $_SESSION["arr"]=array();
    }
    
    if ($_POST) {
       $subarray=array(
           "name"=>$_POST["name"], 
           "surname"=>$_POST["surname"],
           "birthday"=>$_POST["birthday"],  
           "email"=>$_POST["email"]
       );
     $_SESSION["arr"][]=$subarray;
    }
    
    echo "<table border=1><tr><td>Name<td>Surname<td>Email<td>Birthday";
    
    foreach($_SESSION["arr"] as $suba){
       echo "<tr><td>" . $suba["name"]  ;
       echo "<td>" . $suba["surname"] ;
       echo "<td>" . $suba["email"]  ;
       echo "<td>" . $suba["birthday"]  ;
    }
    echo "</table>";
    ?>
    

    However, if you need the data to be persistent (even after the user closes the browser), then you need to store the data say in file format or cookies, etc.

    Login or Signup to reply.
  2. You can use the post values of hidden fields:

     <form action="" method="post">
     <input type="text" name="name" placeholder="name" />
     <input type="text" name="surname" placeholder="surname" />
     <input type="text" name="emailbirthday" placeholder="emailbirthday" />
     <input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
     <button type="reset">Reset Form</button>
     <button type="submit">Submit Form</button>
      
    <?php
    if($_POST["names"] == "")
    {
    $value = $_POST["name"];
    }
    else
    {
    $value = $_POST["names"]."-".$_POST["name"];
    }
    ?>
    <input type="text" name="names"  style='display:none;' value="<?php echo $value ?>">
    </form>
    
    Login or Signup to reply.
  3. If you need to save data persistent and using file to save data is acceptable, i’d use something like that:

    <?php
    $file = 'path/to/file.txt';
    $data = json_decode(file_get_contents($file), true);
    
    if ($_POST) {
        $data[] = [
            "name" => $_POST['name'],
            "surname" => $_POST['surname'],
            "emailbirthday" => $_POST['emailbirthday'],
            "birthday" => $_POST['birthday']
        ];
    }
    
    file_put_contents($file, json_encode($data));
    ?>
    
    <form action="index.php" method="post">
        <input type="text" name="name" placeholder="name" />
        <input type="text" name="surname" placeholder="surname" />
        <input type="text" name="emailbirthday" placeholder="emailbirthday" />
        <input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
        <button type="reset">Reset Form</button>
        <button type="submit">Submit Form</button>
    </form>
    
    <table>
        <tr>
            <th>Name</th>
            <th>Surname</th>
            <th>Emailbirthday</th>
            <th>Birthday</th>
        </tr>
    <?php
        foreach ($data as $row) {
            print '<tr>
                <td>'.$row['name'].'</td>
                <td>'.$row['surname'].'</td>
                <td>'.$row['emailbirthday'].'</td>
                <td>'.$row['birthday'].'</td>
            </tr>';
        }
    ?>
    </table>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search