Skip to content
Advertisement

Can’t figure out how to keep form data on refresh/failed form submission

I need to make a form that keeps the data within its fields if the page were to be refreshed or if the form submission were to fail (not all fields are filled out, some have wrong data, etc.), but I can’t figure out how to do it. I tried looking this up, but nothing I do work.

Page with the form (with the fields I didn’t touch yet removed, only “doctor” has been changed for this functionality), hospitalForm.php:

<?php
  include_once 'header.php';
include_once 'includes/dbh.inc.php';
$sql = "SELECT id, patientName FROM `patientlist`;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);

if (isset($_POST['submit'])) {
    $_SESSION['doctor'] = $_POST['doctor'];
}
?>

<main class="container">
    <form action="includes/hospitalForm.inc.php" method="post">
        <div class="mb-3">
            
        </div>
        <div>
            
        </div>
        <div class="mb-3">
            <label for="exampleInputPassword1" class="form-label">Кто выдал</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="doctor" id="doctor" value="<?php echo isset($_SESSION['doctor']) ? $_SESSION['doctor'] : ''; ?>">
            </div>
        </div>
        <div class="mb-3">

        </div>
        <div class="mb-3">
            
        </div>
        <button type="submit" name="submit" class="btn btn-primary">Ввод</button>
    </form>
</main>

<?php
if (isset($_GET["error"]))
    if ($_GET["error"] == "emptyinput")
        echo "<div style="text-align: center;"><h3>All fields must be filled.</h3></div>";
    elseif ($_GET["error"] == "stmtFailed")
        echo "<div style="text-align: center;"><h3>Something went wrong, try again.</h3></div>";
?>

PHP that processed the form data, hospitalForm.inc.php:

<?php

if (isset($_POST["submit"]))
{
    $scan = "/sample/".$_POST["scan"];
    $patient = $_POST["patient"];
    $doctor = $_POST["doctor"];
    $diagnosis = $_POST["diagnosis"];
    $staytime = $_POST["stayTime"];

    require_once 'dbh.inc.php';
    require_once 'tableFunctions.inc.php';

    if (emptyInputHospital($scan, $patient, $doctor, $diagnosis, $staytime) !== false)
    {
        $_SESSION["doctor"] = $doctor;
        header("location: ../hospitalForm.php?error=emptyinput");
        exit();
    }

    createHospitalRecord($conn, $scan, $patient, $doctor, $diagnosis, $staytime);
}
else
{
    header("location: ../hospitalForm.php");
    exit();
}

Edit: session_start() is called in header.php

Advertisement

Answer

Here are the final versions of the files in question: hospitalForm.php

<?php
include_once 'header.php';
include_once 'includes/dbh.inc.php';
$sql = "SELECT id, patientName FROM `patientlist`;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
?>

<main class="container">
    <form action="includes/hospitalForm.inc.php" method="post">
        <div class="mb-3">
            <label for="exampleInputPassword1" class="form-label">Скан больничного листа</label>
            <div class="col-sm-10">
                <input type="file" class="form-control" name="scan" id="scan">
            </div>
        </div>
        <div>
            <label class="form-label">ФИО пациента</label>
            <div class="input-group mb-3">
                <select class="form-select" name="patient" id="patient" aria-label="Default select example">
                    <?php
                    if ($resultCheck > 0)
                        while ($row = mysqli_fetch_assoc($result))
                        {
                            echo "<option value="$row[id]">$row[patientName]</option>";
                            if ($row['id'] == $_SESSION['patient'])
                                echo "<option selected value="$_SESSION[patient]">$row[patientName]</option>";
                        }
                    ?>
                </select>
        </div>
        <div class="mb-3">
            <label for="exampleInputPassword1" class="form-label">Кто выдал</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="doctor" id="doctor" value="<?php echo isset($_SESSION['doctor']) ? $_SESSION['doctor'] : ''; ?>">
            </div>
        </div>
        <div class="mb-3">
            <label for="exampleInputPassword1" class="form-label">Диагноз</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="diagnosis" id="diagnosis" value="<?php echo isset($_SESSION['diagnosis']) ? $_SESSION['diagnosis'] : ''; ?>">
            </div>
        </div>
        <div class="mb-3">
            <label for="exampleInputPassword1" class="form-label">Число дней больничного листа</label>
            <div class="col-sm-10">
                <input type="number" min="1" class="form-control" name="stayTime" id="stayTime" value="<?php echo isset($_SESSION['staytime']) ? $_SESSION['staytime'] : ''; ?>">
            </div>
        </div>
        <button type="submit" name="submit" class="btn btn-primary">Ввод</button>
    </form>
</main>

<?php
if (isset($_GET["error"]))
    if ($_GET["error"] == "emptyinput")
        echo "<div style="text-align: center;"><h3>All fields must be filled.</h3></div>";
    elseif ($_GET["error"] == "stmtFailed")
        echo "<div style="text-align: center;"><h3>Something went wrong, try again.</h3></div>";
?>

hospitalForm.inc.php

<?php
session_start();

if (isset($_POST["submit"]))
{
    $scan = "/sample/".$_POST["scan"];
    $patient = $_POST["patient"];
    $doctor = $_POST["doctor"];
    $diagnosis = $_POST["diagnosis"];
    $staytime = $_POST["stayTime"];

    require_once 'dbh.inc.php';
    require_once 'tableFunctions.inc.php';

    if (emptyInputHospital($scan, $patient, $doctor, $diagnosis, $staytime) !== false)
    {
        $_SESSION["patient"] = $patient;
        $_SESSION["doctor"] = $doctor;
        $_SESSION["diagnosis"] = $diagnosis;
        $_SESSION["staytime"] = $staytime;
        header("location: ../hospitalForm.php?error=emptyinput");
        exit();
    }

    createHospitalRecord($conn, $scan, $patient, $doctor, $diagnosis, $staytime);
}
else
{
    header("location: ../hospitalForm.php");
    exit();
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement