Question

I want to use back option on php without losing the form data.

I've tried this way;

if ($username==""){
    echo 'Please fill. <input type="button" value="Back" onClick="history.back()">';
} else{
    $doaction;
}

And this way;

$url= $_SERVER['HTTP_REFERER'];
if ($username==""){
    echo "Please fill.";
    header("refresh: 2; url=$url");
}
else{
    $doaction;
}

On both ways I lose my data. How can I make this better?

Was it helpful?

Solution

This worked for me and using ob_start(); (in PHP below) needs to be included, otherwise it will throw an error, such as:

PHP Warning: Cannot modify header information - headers already sent by (output started)

Use the following exactly as shown, then modify to suit later.

Using Sessions is a way to achieve this. Each field will re-appear in the input boxes, if they were filled.

HTML form (form.php)

<?php
session_start();
?>

<form method="post" action="submitted.php">
    <p>
        <label for='name'>Your Name:</label>
        <br>
<input type="text" name="username" value="<?php echo isset($_SESSION['username']) ? $_SESSION['username'] : '' ?>">
    </p>

    <p>
        <label for='name'>Email:</label>
        <br>
<input type="text" name="email" value="<?php echo isset($_SESSION['email']) ? $_SESSION['email'] : '' ?>">
    </p>

    <input type="submit" name="submit" value="Submit">
    <br>
</form>

PHP (submitted.php)

<?php
ob_start();
session_start();

$_SESSION['username'] = $_POST['username'];
$_SESSION['email'] = $_POST['email'];

$url= $_SERVER['HTTP_REFERER'];
$username=$_POST['username'];
$email=$_POST['email'];

// use this one below, if you want to check more than one field
// if (empty($_POST['username']) || empty($_POST['email']) ){

if (empty($_POST['username'])){
echo "Fill in all fields.";
header("refresh: 2; url=$url");
}
else{
echo "OK, redirecting back to show fields are showing, or not.";
header("refresh: 2; url=$url");
}

OTHER TIPS

Your approach reloads the formular, so the webserver will send a clean form to the client. If you want a prefilled form you will have to use a PHP script which knows the formerly filled in values and creates a prefilled form.

In the code you have used, (assuming you have a form further up) there is no data being passed between the pages because nothing is telling the script to transfer the data between that page and it's destination.

In your first example, history.back() simply tells the browser to go back to the last page viewed before the current one. In your second example, your header line essentially just tells the browser to go back to the previous page after two seconds.

To keep data between page transfers, without any further knowledge of what you're doing, I would suggest using JavaScript to fetch your form data and pass it into a URL used in a window.location call (forming a GET request with your form data in it), OR by using javascript to set a cookie containing the data input before you tell the browser to switch page.

In the first solution, you could then use php's $_GET global to access your form data.

In the second solution, you could use javascript to re-populate the form on page load (or at will)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top