How can PHP be used to store form data in a .csv file and then redirect to a different page?

To store form data in a .csv file using PHP, you can first retrieve the form data using the $_POST superglobal array and then write it to a .csv file using functions like fputcsv(). After storing the data, you can use the header() function to redirect to a different page.

<?php
// Retrieve form data
$data = array(
    $_POST['field1'],
    $_POST['field2'],
    $_POST['field3']
);

// Open the .csv file
$fp = fopen('data.csv', 'a');

// Write data to the .csv file
fputcsv($fp, $data);

// Close the file
fclose($fp);

// Redirect to a different page
header('Location: newpage.php');
exit;
?>