CMPF123 - Chapter 6-2

Chapter Overview

  • Subject: PHP: Hypertext Preprocessor for Form Data

  • Course: CMPF 123: Introduction to Web Development

Learning Outcomes

  • Understand how to get data from a form.

  • Display and manipulate data from a form.

Topics Covered

Data Retrieval

  • Retrieve form data using PHP.

  • Manipulate form data using PHP.

PHP Form Handling

  • Use superglobals: GET</code>,<code>_GET</code>, <code>_POST to collect form data.

  • Example:

    • HTML form with method POST.

    • PHP file with method GET.

Examples of HTML Forms & PHP Display

Example 1: Basic Form Handling

  • myForm.html:

    • Contains input fields for Name and Email.

    • Submits data to welcome.php using POST method.

  • welcome.php:

    • Displays user input fetched via $_POST.

Example 2: Complete Form Structure

  • myForm.html:

    • Multiple input types (text, email, url, textarea, radio).

    • Action directs to display.php.

  • display.php:

    • Receives and displays user input via POST superglobal.

Example 3: Additional HTML Form

  • formInsert.html:

    • Gathers nickname, D.O.B., favorite color, hobbies, with proper validation.

    • Submits to mySelf.php.

  • mySelf.php:

    • Processes the input, calculating age and displaying data accurately.

PHP Functions

Date Function

  • Syntax: date(format , timestamp).

  • Common characters:

    • d - Day of the month.

    • m - Month.

    • Y - Year in four digits.

    • l - Day of the week.

JavaScript Validation (Example 3)

  • A script checks at least one checkbox is selected in form.

  • Provides feedback to users for form validation.

Summary

  • PHP effectively handles and manipulates form data using HTML forms.

  • Various examples illustrate how to retrieve data and use it in web applications.