Home > other >  How to insert data from a form into a table in php without using databsaes?
How to insert data from a form into a table in php without using databsaes?

Time:12-05

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.

CodePudding user response:

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.

CodePudding user response:

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>

CodePudding user response:

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>
  • Related