Click this widget to change the font size.
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to PHP and MySQL (University at Buffalo Version)

Chapter 10: Using PHP and MySQL With HTML Forms

You can use PHP to process the user data from a web form.

A Simple Web Page Form

To send the user data to be processed by a back-end PHP prograam you would use the method="POST" and action="name-of-your-php-program.php" attributes on the <form> tag.

<form id="mySampleForm" method="POST" action="process_form.php"
  <legend>Personal Information<legend> 
  <label for="Name">Name:</label> 
  <input id="Name" name="Name" type="text" 
placeholder="Enter your name"/><br class="clear">
  <label for="State">State:</label>
  <Select id="State" name="State">
   <option value="NY">NY</option>
   <option value="OH">Ohio</option>
  </select><br class="clear">
  <label for="GenderM">Gender:</label>
  <input type="radio" id="Gender" name="Gender" value="Male"/> Male<br class="clear">
  <label for="GenderF"></label>
  <input type="radio" name="Gender" value="Female"/> Female<br class="clear">
  <legend>Interests Information<legend>
  <label for="04aCooking">Interests:</label>
  <input type="checkbox" id="04aCooking" name="Cooking"/> Cooking<br class="clear">
  <label for="04bMovies">:</label>
  <input type="checkbox" id="04bMovies" name="Movies"/> Movies<br class="clear">
  <label for="04cSports">:</label>
  <input type="checkbox" id="04cSports" name="Sports"/> Sports<br class="clear">
  <label for="submit"></label>
  <button id="submit" name="submit" type="submit" value="Do it!"/>Do it!</button><br class="clear">
Figure 10-1: HTML <form> Tags

The code above would advise the web browser to render a form like this:

Personal Information

Interests Information Cooking

Figure 10-2: HTML Form

PHP Form Data

When the form is filled out and the "Do it!" button is clicked the browser sends the data to the web server where the process_form.php program can access the $_POST[] superglobal array and process the data.

echo("<b>The \$_POST[] superglobal array contains these elements:</b><br/>\n");
foreach ($_POST as $postElement => $postValue) {
  echo("\$_POST[$postElement] = $postValue<br/>\n");

Figure 10-3: Process PHP Form $_POST[] Data

So, if I had entered my name, selected "New York" as my state, checked Male for my gender and checked movies and sports the $_POST[] superglobal array would look like this:

The $_POST[] superglobal array contains these elements:
$_POST[Name] = Jim Gerland
$_POST[State] = NY
$_POST[Gender] = Male
$_POST[Movies] = on
$_POST[Sports] = on

Figure 10-4: Process PHP Form $_POST[] Data Output

Task - Create a PHP Form Page That Writes to a MySQL Table

For this task you will create a page, php_mysql_form.php on your local web server using your favorite text editor, notepad++ (Windows) or textWrangler (Mac). This file should be based on your index.php file. This web page should:

Once you have tested this page on your local web server, you will upload it to your BSC web space.

You will modify the JavaScript for this item in your index.html page to add a link for this assignment that allows me to view your work.

You *MUST* use the W3C Unicorn Validator to validate your HTML5/CSS3 code.

Let's Get Started with a PHP/MySQL Form Page!

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland