Keep Form Values After Submit in PHP

Hello Developer, If you need to keep form values after submit in PHP then you are at the right place. Here, I will tell you the best methods to implement this feature in your form.

Once You learn to keep input field values after submitting the form. You can easily implement it in your login form registration form,& more.

You should also learn the following examples-

Do not clear Form after submit in PHP

Now the question is Why the need – do not clear the form after submitting input values?

So, Let’s know with the following simple example –

As you know when you click the submit button then the web page will be reloaded and all form input values will be set to the default value or blank.

If you have created a registration form and added validation. When validation of some input fields is failed after clicking the submit button then both valid and invalid values will be removed. After that, you will have to again enter the values. So, It is not good from the user’s point of view.

If you implement a feature to keep all input field values after form submission then you can change only the invalid values.

For PHP 8 or grater

If you use PHP version 8.0 or greater then you can use the following code

Method 1:

<form method="post">

<input type="text" name="firstName" value="<?php echo $_POST['firstName']??''; ?>" placeholder="First Name" />
<input type="text" name="lastName" value="<?php echo $_POST['lastName']??''; ?>" placeholder="Last Name" />
<input type="email" name="email" value="<?php echo $_POST['email']??''; ?>" placeholder="Email Address" />
<input type="city" name="city" value="<?php echo $_POST['city']??''; ?>" placeholder="City" />
<input type="submit" name="submit">

</form>

Method 2:

<?php

$firstName = $_POST['firstName']??'';
$lastName  = $_POST['lastName']??'';
$email     = $_POST['email']??'';
$city      = $_POST['city']??'';

?>
<form method="post">
<input type="text" name="firstName" value="<?php echo $firstName; ?>" placeholder="First Name" />
<input type="text" name="lastName" value="<?php echo $lastName; ?>" placeholder="Last Name" />
<input type="email" name="email" value="<?php echo $email; ?>" placeholder="Email Address" />
<input type="city" name="city" value="<?php echo $city; ?>" placeholder="City" />
<input type="submit" name="submit">

</form>




For PHP 7 or less

If you use PHP version 7.0 or less then you can use the following code

Method 1: 

<form method="post">
<input type="text" name="firstName" value="<?php echo isset($_POST['firstName'])? $_POST['firstName']:''; ?>" placeholder="First Name" /><br></br>
<input type="text" name="lastName" value="<?php echo isset($_POST['lastName'])?$_POST['lastName']:''; ?>" placeholder="Last Name" /><br></br>
<input type="email" name="email" value="<?php echo isset($_POST['email'])?$_POST['email']:''; ?>" placeholder="Email Address" /><br></br>
<input type="city" name="city" value="<?php echo isset($_POST['city'])?$_POST['city']:''; ?>" placeholder="City" /><br></br>
<input type="submit" name="submit">
</form>




Method 2:

<?php

$firstName = isset($_POST['firstName'])? $_POST['firstName']:'';
$lastName = isset($_POST['lastName'])?$_POST['lastName']:'';
$email = isset($_POST['email'])?$_POST['email']:'';
$city = isset($_POST['city'])?$_POST['city']:'';
?>

<form method="post">
<input type="text" name="firstName" value="<?php echo $firstName; ?>" placeholder="First Name" /><br></br>
<input type="text" name="lastName" value="<?php echo $lastName; ?>" placeholder="Last Name" /><br></br>
<input type="email" name="email" value="<?php echo $email; ?>" placeholder="Email Address" /><br></br>
<input type="city" name="city" value="<?php echo $city; ?>" placeholder="City" /><br></br>
<input type="submit" name="submit">
</form>