Home > Back-end >  How to save POST data of a form after user submission without using Sessions, JSON, Ajax, Hidden inp
How to save POST data of a form after user submission without using Sessions, JSON, Ajax, Hidden inp

Time:10-08

First of all I'll be sincere, I'm a student and I've been asked to do a task that seems impossible to me. I don't like asking questions because generally speaking I've always been able to fix my coding issues just by searching and learning, but this is the first time I've ever been on this possition.

I need to create a php file that contains a form with two inputs that the user fills. Once he clicks submit the website will show on top of it the two values. Till here I haven't had an issue, but here's the problem, the next time the user sends another submission, instead of clearing the last 2 values and showing 2 new ones, now there needs to be 4 values showing.

I know this is possible to do through JSON, the use of sessions, Ajax, hidden inputs or using another file (this last one is what I would decide to use if I could), but the teacher says we gotta do it on the same html file without the use of any of the methods listed earlier. He says it can be done through an Array that stores the data, but as I'll show in my example, when I do that the moment the user clicks submit the array values are erased and created from zero. I know the most logical thing to do is asking him, but I've already done that 4 times and he literally refuses to help me, so I really don't know what to do, other than asking here. I should point out that the answer has to be server side, because the subject is "Server-Side Programming".

Thank you for your help and sorry beforehand because I'm sure this will end up being a stupid question that can be easily answered.

For the sake of simplicity I erased everything that has to do with formatting. This is the code:

<?php
    if (isset($_POST['activity']) && isset($_POST['time'])){
        $agenda = array();
        $activity = $_POST['activity'];
        $time = $_POST['time'];
        $text = $activity." ".$time;
        array_push($agenda, $text);
        foreach ($agenda as $arrayData){
            print implode('", "', $agenda);
        }
    }
?>
<html>
    <head>
    </head>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
            <label for="Activity">Activity</label><br>
            <input name= "activity" type="text"><br><br>
            <label for="Time">Time</label><br>
            <input name= "time" type="time"><br><br>
            <input type="submit">
        </form> 
    </body>
</html>

CodePudding user response:

Your question was not very clear to be honest but I might have gotten something going for you.

<?php

$formaction = $_SERVER['PHP_SELF'];
    
if (isset($_POST['activity']) && isset($_POST['time'])){
    $agenda = array();

    //if the parameter was passed in the action url
    if(isset($_GET['agenda'])) {
        $agenda = explode(", ", $_GET['agenda']);
    }

    //set activity time
    $text = $_POST['activity']." ".$_POST['time'];

    //push into existing array the new values
    array_push($agenda, $text);

    //print everything
    print implode(", ", $agenda);

    //update the form action variable
    $formaction = $_SERVER['PHP_SELF'] . "?agenda=" . implode(", ", $agenda);
}

?>

<html>
    <head>
    </head>
    <body>
        <form action="<?php echo $formaction; ?>" method="POST">
            <label for="Activity">Activity</label><br>
            <input name= "activity" type="text"><br><br>
            <label for="Time">Time</label><br>
            <input name= "time" type="time"><br><br>
            <input type="submit">
        </form> 
    </body>
</html>

SUMMARY

Since you cant save the posted values into SESSION vars or HIDDEN input, the next best thing would be to append the previous results of the posted form into the form's action url.

When the form is posted, we verify if the query string agenda exists, if it does we explode it into an array called $agenda. We then concatenate the $_POST['activity'] and $_POST['time'] values and push it to the $agenda array. We then PRINT the array $agenda and update the $formaction variable to contain the new values that were added to the array.

In the HTML section we then set the <form action="" to be <form action="<?php echo $formaction; ?>

  • Related