• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

CodingStatus

- Free Source Code for Web Applications

  • HTML
  • JavaScript
  • jQuery
  • ReactJS
  • Ajax
  • Node.js
  • PHP
  • SQL
  • Interview Questions
  • Installation

Keep Form Values After Submit in PHP

November 15, 2022 By Md Nurullah

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.

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.

Learn Also –

Create a Login Form using PHP and MySQL

Create a Registration Form using PHP and MySQL

To Do List using PHP and MySQL

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>




 

Filed Under: PHP

Hey there, Welcome to CodingStatus. My Name is Md Nurullah from Bihar, India. I'm a Software Engineer. I have been working in the Web Technology field for 3 years. Here, I blog about Web Development & Designing. Even I help developers to build the best Web Applications.

Primary Sidebar

Latest Tutorials

  • How to Install Express Application Using Express Generator Tool
  • Registration Form using Ajax, PHP & MySQL
  • Filter Data with Checkboxes in PHP & MySQL
  • Filter Data by Category in PHP & MySQL
  • Filter Prices From Low to High in PHP, MySQL

Popular Tutorials

  • Filter Data with Checkboxes in PHP & MySQL
  • Filter Data by Category in PHP & MySQL
  • Filter Prices From Low to High in PHP, MySQL
  • To Do List Using PHP and MySQL
  • Upload Multiple Files to Store in MySQL Database Using PHP

Categories

  • Ajax (11)
  • Django (5)
  • HTML (4)
  • Installation (3)
  • Interview Questions (5)
  • JavaScript (20)
  • jQuery (11)
  • Laravel (2)
  • Node.js (23)
  • PHP (42)
  • ReactJS (35)
  • SQL (12)
  • Tips (7)
  • Home
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • Sitemap
  • Contact Us

Copyright © 2023 CodingStatus - All Rights Reserved