module5-Forms.pdf - CS 85 PHP PROGRAMMING Module 5 Forms...

This preview shows page 1 - 4 out of 19 pages.

Module 5: Forms Web Forms Processing Forms Submitting Forms POST & GET Methods filter_var() CS 85: PHP PROGRAMMING Santa Monica College Computer Science & Information Systems Dept.
Forms are, by far, the most common way of interacting with PHP. As we mentioned before, it is recommended that you have basic knowledge of HTML and CSS. If you don't, just head to the HTML Wikibook () or W3School HTML Tutorial ( ) for a refresher. HyperText Markup Language/Forms HTML forms are an easy way to gather data from the end users. Forms are commonly used for account registration, e‐commerce site, guestbook site, content management systems, search boxes, etc. User data can be entered using a variety of input types, from textboxes, drop down menu, radio button controls, etc. Processing the user input requires a server‐side scripting language such as PHP or in some cases when limited interaction is to be provided within a single page a client‐side scripting language such as JavaScript. In this course we will focus on server side processing using PHP. Form Setup To create a form the HTML tag <form> with the attributes action and method is specified as follows: <form method="post" action="action.php"> <!‐‐ Your form here ‐‐> </form> Once the user clicks "Submit", the form body is sent to the PHP script action.php for processing. The values entered in all fields in the form are stored in the associative array variables $_GET or $_POST, depending on the method used to submit the form. The difference between the GET and POST methods is that the GET submits all the values in the URL appended to the action URL an as query string parameters. While POST submits values transparently through HTTP headers. $_GET and $_POST are Superglobal arrays discussed in the next section. Here is a simple form. Notice how it only contains HTML code and is saved with a .html file extension. <!DOCTYPE html > < html > < body > < h2 >HTML Forms</ h2 > < form id= "sampleform" action= "action_page.php" method= "get" > < fieldset > < legend >Personal Information</ legend > < label for= "firstname" >First name</ label > < input type= "text" name= "firstname" id= "firstname" value= "Mickey" > < br > < label for= "lastname" >Last name</ label > < input type= "text" name= "lastname" id= "lastname"
value= "Mouse" > < br >< br > < input type= "submit" value= "Submit" > </ fieldset > </ form > < p >If you click the "Submit" button, the form-data will be sent to a page called "action_page.php".</ p > </ body > </ html > Form Explanation: id The name of the form or control. action The URL of a server‐side script which can process the data. method The method used to send the information. Two methods are supported, POST and GET. POST is the preferred method except for simple searches which generally use GET. Use with server‐side languages. fieldset Form controls are normally contained in a fieldset element. Complex forms may have multiple fieldsets. Fieldsets can contain other fieldsets.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture