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

CodingStatus

- Level Up Your Status with Coding Expertise!

  • Tutorials
    • React Js Tutorials
    • JavaScript Tutorials
    • PHP Tutorials
    • HTML Tutorials
    • SQL Tutorials
    • Laravel Tutorials
  • Snippets
    • React Js Code Snippets
    • Ajax Code Snippets
    • Node.js Code Snippets
    • JavaScript Code Snippets
    • jQuery Code Snippets
    • SQL Code Snippets
  • Programs
    • PHP Program
    • Python Programs
    • Java Programs
  • How To
    • React Js How To
    • Node Js How To
    • Ajax How To
    • PHP How To
    • jQuery How To
    • JavaScript How to
  • Scripts
    • PHP Script
    • Js Script
    • React Js Script
    • JQuery Script
  • Projects
    • PHP Project
  • Interview Questions
  • Guide
  • API Integration
  • 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: Uncategorized

Hello Developer, Welcome to my Blog. I'm Md Nurullah, a software engineer with 5+ years in full-stack web development. As the founder of CodingStatus.com, I'm passionate about sharing coding knowledge and collaborating for a brighter future. Let's connect and code together!

Primary Sidebar

Secondary Sidebar

  • SELECT 3rd Highest Salary in SQL
  • How to create reusable input field in react Js
  • How to Create Reusable Select Field in React Js
  • How to Create Reusable Textarea Field in React Js
  • HTML Paragraph Tag
  • React Class Components with Props
  • Check Armstrong Number in Python
  • PHP echo Statement
  • Check Leap Year in Java
  • Check Leap Year in Python
  • Print prime numbers from 1 to n in python
  • React App with CDN
  • React Js Tutorial
  • Introduction to React Js
  • What is React Js
  • Create React App with NPM
  • React App Folder Structure
  • React Components
  • PHP Variable Scope
  • React Function Component
  • React Function Component with Props
  • React Export and Import Function components
  • React Class Component
  • ReactDOM.render() Method in react js
  • ReactDOM.createRoot() in React Js
  • React Fragment
  • Render Method in React Js – render()
  • How to Check two objects are Equal in JavaScript
  • Dynamic Multi-Step Form in React Js
  • PHP Variables – Step By Step Guide
  • Check Prime Number in Java
  • How to use props in Class Component
  • How to Code HTML
  • Dynamic tree view in React JS
  • JavaScript Word Guessing Game
  • Detect an Internet Connection using JavaScript
  • Simple Calculator in React Js
  • Get Query String Value From URL Using JavaScript
  • How to Insert Form Data Into the Table Using Node.js and MySQL
  • How to display Data from MySQL database table in Node.js
  • Load Records on Select Box using Ajax, PHP & MySQL
  • Swapping of Two Numbers in python
  • How to Install Express Application Using Express Generator Tool
  • Registration Form using Ajax, PHP & MySQL
  • JavaScript Color Picker Chrome Extension
  • To Do List Using PHP and MySQL
  • How to Create JavaScript Accordion
  • Ajax File Upload – Without Refreshing Page
  • JavaScript Password Strength Checker
  • How to Download and Install Node.js on Windows with NPM
  • Home
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • Sitemap
  • Contact Us

Copyright © 2023 CodingStatus - All Rights Reserved