<?php
/**
* Parses form data.
*
* This function extracts data from the $_POST or $_GET superglobals.
* It handles various data types and provides a simple way to access form fields.
*
* @return array Associative array of form data. Returns an empty array if no data is found.
*/
function parseFormData() {
$data = [];
// Check for POST data
if (isset($_POST)) {
foreach ($_POST as $key => $value) {
$data[$key] = trim($value); // Trim whitespace
}
}
// Check for GET data, if POST is not available.
elseif (isset($_GET)) {
foreach ($_GET as $key => $value) {
$data[$key] = trim($value); // Trim whitespace
}
}
return $data;
}
/**
* Example usage:
*/
//Get the parsed form data.
$formData = parseFormData();
//Output the form data to the screen (for testing).
echo "<pre>";
print_r($formData);
echo "</pre>";
?>
Add your comment