How can PHP be used to divide a form into multiple pages?

To divide a form into multiple pages using PHP, you can use sessions to store the form data as the user progresses through each page. Each page of the form can be a separate PHP file that submits the data to the next page or stores it in the session. This allows the user to navigate through the form step by step without losing any previously entered data.

<?php
session_start();

// Check if form data has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Store form data in session
    $_SESSION["page1_data"] = $_POST["page1_data"];
    $_SESSION["page2_data"] = $_POST["page2_data"];
    
    // Redirect to next page
    header("Location: page2.php");
    exit();
}
?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <!-- Form fields for page 1 -->
    <input type="text" name="page1_data" />
    
    <input type="submit" value="Next" />
</form>