How can I maintain state across pages using $_POST?
803 Aug 2024
To maintain state across pages with $_POST
, you typically use sessions or hidden fields. Sessions allow you to store user data across multiple pages. Here’s how to use sessions:
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$_SESSION["formData"] = $_POST;
header("Location: next_page.php");
exit;
}
?>
In next_page.php
, retrieve the data:
<?php
session_start();
if (isset($_SESSION["formData"])) {
$formData = $_SESSION["formData"];
echo "Form Data: ";
print_r($formData);
}
?>
Using hidden fields is another approach, but sessions are generally more secure and flexible for maintaining state across pages.
0 likes
Top related questions
Related queries
Latest questions
दो देशों में धरती हिली! रूस के कुरील द्वीप समूह और अफ़ग़ानिस्तान में 6.8 तीव्रता का भूकंप
02 Aug 2025 2
चीन में बच्चा पैदा करने पर ₹1.30 लाख देगी सरकार
31 Jul 2025 0
अंतरिक्ष में भारत रचेगा इतिहास: NASA-ISRO का निसार मिशन आज होगा लॉन्च, पृथ्वी की करेगा निगरानी
30 Jul 2025 0