How can PHP developers allow users to edit their input data after submitting a form?
To allow users to edit their input data after submitting a form, PHP developers can save the submitted data in session variables and pre-fill the form fields with this data when the form is displayed again. This allows users to make changes to their input before submitting it again.
<?php
session_start();
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Save form data in session variables
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
// Add more fields as needed
// Redirect to the form page to display the form with pre-filled data
header("Location: form.php");
exit();
}
// Display the form with pre-filled data if available
$name = isset($_SESSION['name']) ? $_SESSION['name'] : '';
$email = isset($_SESSION['email']) ? $_SESSION['email'] : '';
// Add more fields as needed
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name" value="<?php echo $name; ?>" placeholder="Name">
<input type="email" name="email" value="<?php echo $email; ?>" placeholder="Email">
<!-- Add more fields as needed -->
<button type="submit">Submit</button>
</form>