Skip to content
PHP Logo

How can PHP and HTML be used together to pass form data between multiple pages?

To pass form data between multiple pages using PHP and HTML, you can use the POST method to submit the form data to a PHP script on the first page. Then, you can store the form data in session variables and redirect to the second page. Finally, on the second page, you can retrieve the form data from the session variables and display it as needed.

php
// First Page (form.php)
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $_SESSION['formData'] = $_POST;
    header('Location: second_page.php');
    exit();
}

// Second Page (second_page.php)
session_start();
if(isset($_SESSION['formData'])) {
    $formData = $_SESSION['formData'];
    // Display form data as needed
}