phphtmlforms

How can the updated version of a form element be displayed?


I am writing php/html to present a table of information to the user and allow him/her to update it. The data is stored in an SQL database. Storing and retrieval from the database works fine. The problem I have is how to display the updated data back to the user. To illustrate my difficulty I have reduced the code to update just one value. This code comprises 3 files:

  1. simple.php which reads the initial value from the user and presents it on the webpage in a form element.
  2. update.php which is called by simple.php to update the database.
  3. accessDBtable.php which is called by the other two files to access the database.

This all works fine but after the user hits the submit button in the form element (s)he is left looking at whatever is output by update.php.

What I have been unable to find out is how make control flow back to simple.php so that it can display the form element with the new value.

<!DOCTYPE html>
<html>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!--     File: simple.php    -->
<!--    Input: From user -->
<!--   Output: To user and database -->
<!-- Function: Read original value from database using accessDBtable.php and display it -->
<!--           Read new value given by user  -->
<!--           Write new value to database by using updateDB.php -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
    <body>
    <?php
        //Read initial value from database and store it in $sDate_G
        $sSQLcommand_G = "SELECT * FROM Rota WHERE Id = '0'";   
        include 'deleted'; 
        $asFields = $cDBresults_G->fetch( PDO::FETCH_ASSOC );  
        $sDate_G = $asFields[ 'Date' ];
    ?>
        <FORM action="updateDB.php"> 
        <label for="date">Date:</label>
    <?php
        echo "<input type='text' id='date' name='date' value='$sDate_G'>";
    ?>
        <INPUT type="submit" value="Save Updates" class="sub-but">
    </body>
</html>


Solution

  • You're very close! What you need is to redirect the user back to simple.php after the update is done in updateDB.php.

    PHP provides a built-in way to do this using the header() function. Here’s how you can do it:

    In your updateDB.php file, after updating the database, simply add this:

    header("Location: simple.php");
    exit();
    

    This will redirect the user back to simple.php, where the updated value will be fetched from the database and displayed.

    ✏️ Example Here’s how your updateDB.php might look:

    <?php
    // Get the new value from the form (GET or POST based on your form method)
    $sNewDate = $_GET['date']; // Or use $_POST if you change the method
    
    // Include DB connection file
    include 'accessDBtable.php';
    
    // Update the database
    $sSQLcommand = "UPDATE Rota SET Date = :date WHERE Id = '0'";
    $stmt = $pdo->prepare($sSQLcommand);
    $stmt->execute(['date' => $sNewDate]);
    
    // Redirect back to the form page to show updated value
    header("Location: simple.php");
    exit();
    ?>
    

    🧠 Bonus Tip: Use POST instead of GET To avoid showing form data in the URL, consider changing your form to use POST:

    In simple.php:

    <FORM action="updateDB.php" method="POST">
    

    In updateDB.php:

    $sNewDate = $_POST['date'];
    

    This way, your form submits, updates the database, and cleanly redirects back to the form with the new value.