CITS1231 Web Technologies PHP Form Processing

Objectives • Understand GET and POST methods • Write a PHP program to accept form data • Use PHP to give useful feedback on form data • Use PHP to preserve data in submitted forms

3

Recall how to create forms …… … … …

4

Input Types •

• text (=default) is a one-line text input box

• button, checkbox, file, radio etc

• reset and submit (buttons)

• textareas and selection are other possible input elements in a form. 5

Form Data • When the form data is submitted the client sends to the server a message conveying the list of input element names and current values. • Eg: – Userdata1: Mark – Userdata2: Reynolds – Checkbox1: on

6

HTTP Methods • The Hypertext Transfer Protocol (HTTP) is a communications protocol for the transfer of information (especially web pages) on the Internet. • Clients send HTTP requests to servers. • There are several types of request messages (specified in the header of the message). • A message also has an optional message body. • GET and POST are two common HTTP message types.

7

GET vrs POST • GET messages are designed for requests for information from the server. • These should be relatively short, plain text requests. • POST messages are designed for sending information to the server for it to store and/or process. • POST messages can be large and can contain a wider variety of formats encoded.

8

GET Method •

A GET message is sent to a URI consisting of the “action” of the form, followed by a “?” followed by a string of input names = values as follows: http://some_server_address?userdata1=Mark&userdat2=Reyn olds&Checkbox1=on



The server program at the address http://some_server_address

is given the string after the “?” (called the form data set) to pull apart and process.

9

POST Method • Here the form data set is simply put in the body of the message for the server program to process. • In the rest of this lecture we will just look at how to use PHP programs on POSTed data. • However, it is also convenient to use PHP to process form data sent via the GET method.

10

Processing POST data • It is very easy to write PHP code to use the form values returned by the user (via the POST method) • The $_POST[] array is a predefined PHP variable initialised with the submitted values as strings. • If an input named “xxx” is given a value “yyy” by the user then $_POST[“xxx”] will contain the value “yyy”.

11

Example of Form proc.php

form.html