5
Posted December 22, 2012 by Victor in PHP Programming
 
 

How to Use HTML Forms with PHP

Man Filling out Tax Form
Man Filling out Tax Form

HTML form validation with PHP is another interesting topic that should arouse the attention of every programmer. HTML forms are often used to capture user details via a browser. There are many parts of a HTML form which are validated when passed into a php script. Here are the parts of a HTML form and how they are created:

  • The Text Boxes: These are used to capture text input done by a user. Most of the time, text boxes are provided for input such as name, address and e-mail of an individual. For instance, to code a textboxes that accept the name of a person, his address and e-mail address, use:
<form action="victor.php" method="post">
Name <input type="text" name="entername" value="" />

Address <input type="text" name="enteraddress" value="" />

Email <input type="text" name="enteremail" value="" />
</form>
  • Radio Buttons: These are used to make a single selection among many choices. They are often round in shape and give you a choice of selecting only one choice. Here is how they are coded in HTML:
<form action="victor.php" method="post">
<input type="radio" name="sex" value="male" /> Male
<input type="radio" name="sex" value="female" />Female
</form>

The name of the radio buttons are often the same because radio buttons are often treated as a group. However, the value attribute for these buttons are different to indicate the difference in selections that each button represents.

  • Select List: This provides a list of choices from which you can make either single or multiple selections. Here is how it is written in HTML:
<form action="victor.php" method="post"><select multiple="multiple" name="Coursework[]" size="4"><option value="maths">Maths</option><option value="english">English</option><option value="chemistry">Chemistry</option><option value="physics">Physics</option>
</select>
</form>

The select option has an array due to the fact that you can have multiple selections done at the same time.

  • Checkbox: The checkbox will be used while performing either single or multiple selections between varieties of choices. Here is how to code the checkbox in HTML:
<form action="victor.php" method="“post”">
<input type="checkbox" name="freshman" value="no" />Freshman

<input type="checkbox" name="sophomore" value="no" />Sophomore

<input type="checkbox" name="junior" value="no" />Junior

<input type="checkbox" name="senior" value="no" />Senior
</form>

  • Submit Button: This is the button that will trigger the submission of the whole HTML form into your PhP backend for scripting. Here is how it is coded:
<form action="victor.php" method="“post”">
<input type="submit" name="submitinfo" value="Submit" />
</form>

Capturing the HTML Form Contents with PhP

When the user has filled the above parts of a HTML form with relevant information, he will click on the submit button. The form will be submitted due to the POST method into a PhP file named victor.php. Here are the basic php commands for capturing contents from a HTML form:

  • $_SERVER[“REQUEST_METHOD”]: This is used to check that the form has really been submitted. If the REQUEST_METHOD is POST, then, the script has been submitted.
  • $_GET: This is used for detecting a form submission. It is a PhP script that gets information from a HTML form. The $_GET is often used together with another PhP function known as isset(). For instance, you can tell php that:
<form action="victor.php" method="get">
<?php if (isset(&_GET["submit"]))  { process the contents of the form. } ?>
</form>

This simply tells PhP that when the submit button is clicked and the form is set to GET, then, php should process the contents of the HTML form.

  • Form Validation with PhP

When the contents are captured, the next thing will be to try and validate the contents which are by default stored in either the $_POST or $_GET arrays. Should the user not have an entry as per specifications, there need to be an error message. For instance, if a user does not enter his name, there needs to be an error telling him that the name textbox is blank and he needs to adjust the input and submit the form once more. This is the process of form validation with PhP.


Victor