1. <?php
  2. /**
  3. * Parses form data.
  4. *
  5. * This function extracts data from the $_POST or $_GET superglobals.
  6. * It handles various data types and provides a simple way to access form fields.
  7. *
  8. * @return array Associative array of form data. Returns an empty array if no data is found.
  9. */
  10. function parseFormData() {
  11. $data = [];
  12. // Check for POST data
  13. if (isset($_POST)) {
  14. foreach ($_POST as $key => $value) {
  15. $data[$key] = trim($value); // Trim whitespace
  16. }
  17. }
  18. // Check for GET data, if POST is not available.
  19. elseif (isset($_GET)) {
  20. foreach ($_GET as $key => $value) {
  21. $data[$key] = trim($value); // Trim whitespace
  22. }
  23. }
  24. return $data;
  25. }
  26. /**
  27. * Example usage:
  28. */
  29. //Get the parsed form data.
  30. $formData = parseFormData();
  31. //Output the form data to the screen (for testing).
  32. echo "<pre>";
  33. print_r($formData);
  34. echo "</pre>";
  35. ?>

Add your comment