Home > Net >  How to save form data with PHP without using Sessions, Databases or Files
How to save form data with PHP without using Sessions, Databases or Files

Time:11-12

I'm learning PHP and my teacher told about an exercise in which I have to create an agenda using a form to take data and change or delete the info.

It looks quite easy when use the obvious but, unfortunately I've been banned from use sessions, databases or files, so, I've been having some issues with saving the data.

The teacher said to use json_encode in an input type hidden in order to get the data, but even if I manage to save the submitted info, it will end up changing to the next info.

The big thing is to save form data using json_encode in a input hidden, but after that I don't know how to preserve it.

By the way, I have all of that in the same file, I have another with some utils functions.

In the PHP file I have some functions I haven't use since, first of all, I want it to add the info without removing the above and then the rest.

<----------------- EDIT ----------------->

Here is the modules.php:

function getLocalTime() {
    date_default_timezone_set('Atlantic/Canary');
    return date("d-m-Y H:i:s a");
}

function encodeData($data) {
    return json_encode($data);
}

function getData() {
    if(isset($_POST['hiddenInput'])) {
        return json_decode($_POST['hiddenInput'], true);
}
    return [];
}

function addData(&$data) {
    $date = getLocalTime();
    $data[$_POST['regNid']]['name'] = $_POST['regName'];
    $data[$_POST['regNid']]['surname'] = $_POST['regSurname'];
    $data[$_POST['regNid']]['email'] = $_POST['regEmail'];
    $data[$_POST['regNid']]['phone'] = $_POST['regTel'];
    $data[$_POST['regNid']]['birthday'] = $_POST['regBirth'];
    $data[$_POST['regNid']]['addDate'] = $date;
    $data[$_POST['regNid']]['block'] = false;
}

And new form code:

<?php            
    if(!isset($_POST['hiddenInput'])) {
        $agenda = [];
    } else {
        $agenda = getData();
        // var_dump($agenda);
        addData($agenda);
    }
    print('<pre>'.print_r($agenda, true).'</pre>');
?>
    <form  action='<?php echo $_SERVER['PHP_SELF']; ?>' method="POST" enctype="multipart/form-data" role="form" >
        <div >
            <div >
                <input type="text" id="registerNid" name="regNid"  placeholder="DNI"/>
                <label for="registerName">DNI</label>
            </div>
            <div >
                <input type="text" id="registerName" name="regName"  placeholder="Nombre"/>
                <label for="registerEmail">Nombre</label>
            </div>
            <div >
                <input type="text" id="registerSurname" name="regSurname"  placeholder="Apellidos"/>
                <label for="registerUsername">Apellidos</label>
            </div>
            <div >
                <input type="email" id="registerEmail" name="regEmail"  placeholder="Email"/>
                <label for="registerUsername">Email</label>
            </div>
            <div >
                <input type="tel" id="registerTel" name="regTel"  placeholder="Teléfono"/>
                <label for="registerPassword">Teléfono</label>
            </div>
            <div >
                <input type="text" id="registerBirth" name="regBirth"  placeholder="Fecha de nacimiento"/>
                <label for="registerBirth">Fecha de nacimiento</label>
            </div>
        </div>
        <div >
            <button type="submit" id="submit"  name="sub-register" value="register">Registrarse</button>
            <?php
                $data = [$_POST["regNid"], $_POST["regName"], $_POST["regSurname"], $_POST["regEmail"], $_POST["regTel"], $_POST["regBirth"]];
            ?>
            <input type="hidden" name="hiddenInput" value='<?php echo encodeData($data); ?>'>
            <div >
                <p>¿Ya existe? <a  href="#!">Actualizar datos</a></p>
            </div>
        </div>
    </form>


<?php            
if(!isset($_POST['hiddenInput'])) {
    $agenda = [];
} else {
    $agenda = getData();
    // var_dump($agenda);
    addData($agenda);
}
print('<pre>'.print_r($agenda, true).'</pre>');
?>
<form  action='<?php echo $_SERVER['PHP_SELF']; ?>' method="POST" enctype="multipart/form-data" role="form" >
    <div >
        <div >
            <input type="text" id="registerNid" name="regNid"  placeholder="DNI"/>
            <label for="registerName">DNI</label>
        </div>
        <div >
            <input type="text" id="registerName" name="regName"  placeholder="Nombre"/>
            <label for="registerEmail">Nombre</label>
        </div>
        <div >
            <input type="text" id="registerSurname" name="regSurname"  placeholder="Apellidos"/>
            <label for="registerUsername">Apellidos</label>
        </div>
        <div >
            <input type="email" id="registerEmail" name="regEmail"  placeholder="Email"/>
            <label for="registerUsername">Email</label>
        </div>
        <div >
            <input type="tel" id="registerTel" name="regTel"  placeholder="Teléfono"/>
            <label for="registerPassword">Teléfono</label>
        </div>
        <div >
            <input type="text" id="registerBirth" name="regBirth"  placeholder="Fecha de nacimiento"/>
            <label for="registerBirth">Fecha de nacimiento</label>
        </div>
    </div>
    <div >
        <button type="submit" id="submit"  name="sub-register" value="register">Registrarse</button>
        <?php
            $data = [$_POST["regNid"], $_POST["regName"], $_POST["regSurname"], $_POST["regEmail"], $_POST["regTel"], $_POST["regBirth"]];
        ?>
        <input type="hidden" name="hiddenInput" value='<?php echo encodeData($data); ?>'>
        <div >
            <p>¿Ya existe? <a  href="#!">Actualizar datos</a></p>
        </div>
    </div>
</form>

My main goal is to save the data as this:

Array(
  [99999999P] => Array
    (
        [name] => Daniel
        [surname] => García
        [email] => [email protected]
        [phone] => 678678678
        [birthday] => 1980/02/28
        [addDate] => 11-11-2022 12:16:49 pm
        [blocked] => 
    )
)

But I want to save ALL the data I submit in the form, so the Array doesn't reset everytime I submit the info; the final result could be something like this.

Array(
  [99999999P] => Array
    (
        [name] => Daniel
        [surname] => García
        [email] => [email protected]
        [phone] => 678678678
        [birthday] => 1980/02/28
        [addDate] => 11-11-2022 12:16:49 pm
        [blocked] => 
    )
  [77777777L] => Array
    (
        [name] => Lucia
        [surname] => Santana
        [email] => [email protected]
        [phone] => 99999999
        [birthday] => 1990/06/20
        [addDate] => 11-11-2022 12:18:35 pm
        [blocked] => 
    )
)

But I'm getting this: enter image description here

And it only is saving two user info, when I try to store the third it remove the first.

CodePudding user response:

This area of your code is causing issues:

<?php
    $data = [$_POST["regNid"], $_POST["regName"], $_POST["regSurname"], $_POST["regEmail"], $_POST["regTel"], $_POST["regBirth"]];
?>
<input type="hidden" name="hiddenInput" value='<?php echo encodeData($data); ?>'>

If I replace it with the following code it works:

<input type="hidden" name="hiddenInput" value='<?php echo encodeData($agenda); ?>'>

Explanation

The line:

$data = [$_POST["regNid"], $_POST["regName"], ....

Is not organising the array in the form that you want, and unnecessary. The $agenda variable already has the correct data at this point, so just using that for the hiddenInput field seems appropriate.

  • Related