Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="author" content="Wendi Jollymore">
- <meta name="description" content="a quick program to display form data">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Your Form Data</title>
- <style>
- html, body {
- font: 16px Arial, sans-serif;
- }
- table, header, footer {
- margin: 7px;
- }
- header, footer {
- background-color: #ecf7f9;
- border: 1px solid #099;
- padding: 10px;
- }
- footer {
- text-align: center;
- }
- h1 {
- border-bottom: 2px solid #099;
- color: #099;
- font-size: 1.5em;
- }
- table {
- border: 2px solid #099;
- border-collapse: collapse;
- }
- td, th {
- border: 1px solid #099;
- padding: 5px;
- }
- th {
- background: #099;
- color: white;
- }
- p, pre {
- margin: 10px;
- }
- .note {
- font-size: .85em;
- font-style: italic;
- }
- .error {
- color: red;
- }
- </style>
- </head>
- <body>
- <header>
- <h1>Your Form Data</h1>
- <p>This is the form data that was sent to me:</p>
- </header>
- <?php
- // store the raw array based on post method
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $raw = $_POST;
- } else if ($_SERVER['REQUEST_METHOD'] === 'GET') {
- $raw = $_GET;
- } else { // only get or post
- $errors[] = "Sorry, I can only process forms that use "
- ."GET or POST.";
- }
- // if we got an array, filter it
- if (isset($raw)) {
- // for each item in the array, parse into html-friendly values
- foreach ($raw as $name => $value) {
- // if this element is an array of values, filter each element
- if (is_array($value)) {
- foreach ($value as $val) {
- $temp[] = htmlspecialchars($val);
- }
- // assign filtered values to an array element
- $inputs[htmlspecialchars($name)] = $temp;
- } else { // not an array, this is a single value
- // assign filtered value to an array element
- $inputs[htmlspecialchars($name)] = htmlspecialchars($value);
- }
- }
- } else { // empty array (might be because of unsupported method)
- $errors[] = "No form data received.";
- }
- // display any errors
- if (isset($errors) && count($errors) > 0) {
- echo "<p class='error'>";
- foreach ($errors as $err) {
- echo "$err<br>\n";
- }
- echo "</p>\n";
- // no errors, display form inputs in a table
- } else {
- echo "<table>\n<tr><th>Field</th><th>Value</th></tr>\n";
- foreach ($inputs as $name => $value) {
- // first column is name
- echo "<tr>\n<td>".$name."</td>\n<td>\n";
- // second column is value(s)
- // if this element is an array of values
- if (is_array($value)) {
- foreach ($value as $val) {
- echo $val."<br>\n";
- }
- } else { // single value
- echo $value;
- }
- echo "</td>\n</tr>\n";
- }
- echo "</table>\n";
- }
- ?>
- <p class="note">Note: elements that hold multiple values must
- be defined as arrays in order to be processed properly
- by PHP e.g. name="lstAnimals[]"</p>
- <p>Array Data (the array of form data received - this
- might help in solving some problems with your form):</p>
- <pre><?= var_dump($inputs) ?></pre>
- <footer>
- <address>© 2018 Wendi Jollymore, Sheridan College</address>
- </footer>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement