Advertisement
ProfWendi

Very Basic PHP Form Processor

Aug 31st, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.32 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8">
  5.         <meta name="author" content="Wendi Jollymore">
  6.         <meta name="description" content="a quick program to display form data">
  7.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8.         <title>Your Form Data</title>
  9.         <style>
  10.           html, body {
  11.             font: 16px Arial, sans-serif;
  12.           }
  13.           table, header, footer {
  14.             margin: 7px;
  15.           }
  16.           header, footer {
  17.             background-color: #ecf7f9;
  18.             border: 1px solid #099;
  19.             padding: 10px;
  20.           }
  21.           footer {
  22.             text-align: center;
  23.           }
  24.           h1 {
  25.             border-bottom: 2px solid #099;
  26.             color: #099;
  27.             font-size: 1.5em;
  28.           }
  29.           table {
  30.             border: 2px solid #099;
  31.             border-collapse: collapse;
  32.           }
  33.           td, th {
  34.             border: 1px solid #099;
  35.             padding: 5px;
  36.           }
  37.           th {
  38.             background: #099;
  39.             color: white;
  40.           }
  41.           p, pre {
  42.             margin: 10px;
  43.           }
  44.           .note {
  45.             font-size: .85em;
  46.             font-style: italic;
  47.           }
  48.           .error {
  49.             color: red;
  50.           }
  51.         </style>
  52.     </head>
  53.     <body>
  54.       <header>
  55.         <h1>Your Form Data</h1>
  56.         <p>This is the form data that was sent to me:</p>
  57.       </header>
  58.  
  59.         <?php
  60.         // store the raw array based on post method
  61.         if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  62.           $raw = $_POST;
  63.            
  64.         } else if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  65.           $raw = $_GET;      
  66.          
  67.         } else {  // only get or post
  68.           $errors[] = "Sorry, I can only process forms that use "
  69.             ."GET or POST.";
  70.         }
  71.        
  72.         // if we got an array, filter it
  73.         if (isset($raw)) {
  74.  
  75.           // for each item in the array, parse into html-friendly values
  76.           foreach ($raw as $name => $value) {
  77.              
  78.               // if this element is an array of values, filter each element
  79.               if (is_array($value)) {
  80.                 foreach ($value as $val) {
  81.                    $temp[] = htmlspecialchars($val);
  82.                 }
  83.                 // assign filtered values to an array element
  84.                 $inputs[htmlspecialchars($name)] = $temp;
  85.  
  86.               } else {  // not an array, this is a single value
  87.                 // assign filtered value to an array element
  88.                 $inputs[htmlspecialchars($name)] = htmlspecialchars($value);
  89.               }
  90.           }
  91.         } else { // empty array (might be because of unsupported method)
  92.           $errors[] = "No form data received.";
  93.         }
  94.        
  95.         // display any errors
  96.         if (isset($errors) && count($errors) > 0) {
  97.           echo "<p class='error'>";
  98.           foreach ($errors as $err) {
  99.             echo "$err<br>\n";
  100.           }
  101.           echo "</p>\n";
  102.  
  103.         // no errors, display form inputs in a table
  104.         } else {
  105.                
  106.           echo "<table>\n<tr><th>Field</th><th>Value</th></tr>\n";
  107.           foreach ($inputs as $name => $value) {
  108.            
  109.               // first column is name
  110.               echo "<tr>\n<td>".$name."</td>\n<td>\n";
  111.              
  112.               // second column is value(s)
  113.              
  114.               // if this element is an array of values
  115.               if (is_array($value)) {
  116.                 foreach ($value as $val) {
  117.                   echo $val."<br>\n";
  118.                 }
  119.               } else {  // single value
  120.                 echo $value;
  121.               }
  122.               echo "</td>\n</tr>\n";
  123.           }
  124.           echo "</table>\n";
  125.        
  126.         }
  127.         ?>
  128.      
  129.       <p class="note">Note: elements that hold multiple values must
  130.       be defined as arrays in order to be processed properly
  131.       by PHP e.g. name="lstAnimals[]"</p>
  132.      
  133.       <p>Array Data (the array of form data received - this
  134.       might help in solving some problems with your form):</p>
  135.      
  136.       <pre><?= var_dump($inputs) ?></pre>
  137.      
  138.       <footer>
  139.         <address>&copy; 2018 Wendi Jollymore, Sheridan College</address>
  140.       </footer>
  141.     </body>
  142. </html>
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement