XHTML & Forms
PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML – id is optional (but encouraged in XHTML) action is mandatory. The value is the URL (or name of the file, if in current directory) where the data collected by the form is to be sent method can take one of two values: get and post 2
HTML forms HTML forms (a way of getting input from the browser) include form elements such as: – text areas – password areas – check boxes – selection lists – scrolled selection lists – radio buttons – submit button – reset button – picture buttons 3
An HTML form 4
5
Using method=“get” This method appends the form-data to the URL in name/value pairs This method is useful for form submissions where a user want to bookmark the result (think google search) There is a limit to how much data you can place in a URL (varies between browsers), therefore, you cannot be sure that all of the form-data will be correctly transferred Never use the "get" method to pass sensitive information! (password or other sensitive information will be visible in the browser's address bar) 6
Using method=“post” This method sends the form-data as an HTTP post transaction Form submissions with the "post" method cannot be bookmarked (think login forms or member only areas) The "post" method is more robust and secure than "get", and "post" does not have size limitations 7
PHP Superglobals $_GET $_POST Works in the same way as $_GET – $_POST is an associative array – The keys to the array are the same as the field names in your form – Data sent via post is not exposed to the browser. The transaction is handled internally by the webserver 8
HTML form tags - overview 9
Starting and ending forms - tag HTML forms are created by using the HTML and tags Within these tags, you place various HTML form elements, such as text areas, check boxes, and radio buttons.. ---(Your FORM elements here). 10
Form arguments The tag has two primary arguments: action - Specifies the URL of the PHP script to start when the form is submitted method - Defines the format that will be used to send data to the PHP script – get appends the form arguments to the end of the Web address (as a Query String after the “?”) – post sends the data as part of the body of the HTTP Request 11
The tag Used to define a range of form elements, designated by its TYPE attribute. The general format of the tag is: – type = submit | reset | text | checkbox | radio| etc. – name: will be used by the CGI program to identify the form element (i.e. the CGI variable) – value: the value to be associated with “name” when processing the form (The tag has NO closing tag ) 12
The tag - Form buttons Used to submit the form or erase all input When the submit button is clicked, the form data is sent to the program specified in the ACTION argument, and the CGI program executed If form consists only of a submit button, the CGI program is simply executed 13
tag - Text box The text input type creates text boxes on forms 14 text area box size max. characters allowed CGI variable name
tag - Text box 15
Text areas - tag 16 u Enables the user to enter multiple lines of text Please enter comments here u The wrap attribute specifies what to do if the user types beyond the right margin of the text area If omitted, wrapping is not enabled If wrap=“virtual”, wrapping is enabled but the text is submitted without linebreaks If wrap=“physical”, line breaks are submitted as part of the text CGI variable name default text value
tag - Checkboxes 17 Copyright © 2002 Pearson Education, Inc.
tag - Checkboxes Small boxes on a form that create a check mark when the user clicks them USA China The user may select (tick) multiple boxes 18 CGI variable name CGI variable value when clicked label displayed next to checkbox
tag - Radio buttons 19 Copyright © 2002 Pearson Education, Inc.
Radio buttons - tag Small circles that operate similarly to checkboxes, except that only one button within the group can be selected at any given time Visa American Express Mastercard 20 CGI variable name CGI variable value label displayed next to radio button
tag - Selection lists Used to create a list for user to choose from, either as a drop- down menu or a scrolling box: Win95 Win98 Win2k linux For a scrolling box, specify SIZE greater than one. e.g. SIZE = 3 will generate a scrolling windows 3 rows high 21 CGI variable name list items that user can select CGI variable value
Form generation using PHP - Example 22
Form generation using PHP - Example - output 23
Example form Selection list Radio buttons Text box Text area Text box Submit button