How can session variables be used to store form data in PHP for multi-page forms?
Session variables can be used to store form data in PHP for multi-page forms by storing the form data in session variables on each form submission. This allows the data to persist across multiple pages until the final submission. By storing the form data in session variables, you can easily retrieve and manipulate the data as needed throughout the form submission process.
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$_SESSION['form_data'] = $_POST;
// Redirect to the next page in the form sequence
header("Location: next_page.php");
exit;
}
// Retrieve form data from session variables
if(isset($_SESSION['form_data'])) {
$form_data = $_SESSION['form_data'];
}
?>