What is a more elegant solution to handling form submissions and setting cookies in PHP?
When handling form submissions and setting cookies in PHP, a more elegant solution is to separate the logic for processing the form data and setting the cookies into different functions. This helps improve code readability and maintainability by breaking down the tasks into smaller, more manageable parts.
<?php
// Function to process form submission
function processFormSubmission() {
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process form data
$formData = $_POST['formData'];
// Call function to set cookies
setCookies($formData);
}
}
// Function to set cookies
function setCookies($data) {
// Set cookies based on form data
setcookie('cookieName', $data, time() + 3600, '/');
}
// Call function to process form submission
processFormSubmission();
?>
Keywords
Related Questions
- How can a loop or iteration be used in PHP to separate and insert individual array elements into separate rows in a database table?
- How can PHPMailer be configured to correctly display Umlauts and special characters in the FromName field?
- What are best practices for securely storing images without using a database in PHP?