Why is the data coming from the form only displayed after the resubmission of the form?

Asked

Viewed 21 times

-1

I am developing a task manager. Obviously I need a task add function.

function loadInputForm () {
    global $title, $status,$description, $date;

    if($_POST){
        $title = filter_input(INPUT_POST, "titulo");
        $status = filter_input(INPUT_POST, "status");
        $description = filter_input(INPUT_POST, "descricao");
        $date = filter_input(INPUT_POST, "data");
    }

    if($title){
        $taskInput = "$title%$status%$description%$date\n";
        addTasks($taskInput);
    }
}

With the above code I receive the form information and save it in a string with the tab "%".

 function addTasks ($taskInput) {
    $fileOpen = fopen(PATH, 'a');
    fwrite($fileOpen, $taskInput);
    fclose($fileOpen);
}

Here I open a file with a native PHP function and write the string with the form data in that file with the fwrite() function. For the interface I simply break the text of this file with strings handling functions and display them to the user.

The problem is that when the user finishes filling the form and presses the send data button, the page is updated but the content is not added. It only appears in the interface after refreshing the page again. Hence another problem arises, after this second update of the page, the form is resubmitted, and the data previously typed by the user is again written in the file, causing a repetition of content.

1 answer

0

Probably the code that reads the data is running before of function addTask() be executed. That’s why it only appears after the page reloads. Try to run it before, or move the function addTask() to another address.

For example:

  • You are on the page exemplo.com/tasks.
  • The form makes the Submit to exemplo.com/tasks/add.
  • After the data is saved, the user is redirected back to /task.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.