HTML Form and PHP IST2101
2 Review of Previous Class HTML table and PHP array Winner is chosen randomly using rand() function.
Today’s Objectives Learn interaction with users using HTML Form IST2103
Why Are Forms Important to You? Database query will be sent through forms –Keyword –A set of possible values Database query conditions will be specified through forms –Including or excluding a condition IST2104
Input Parameter Using Form Pick a student (0-4)! <?php $lastname = array ('Smith', 'Kim', 'Davis', 'Miller', 'Taylor'); $firstname = array('John', 'Anthony', 'Richard', 'Thomas', 'Jessica'); if (!empty($_POST['studentID'])){ $v = $_POST['studentID']; echo "Today's Winner is: ".$firstname[$v]." ".$lastname[$v]; } ?> IST2105
Input Parameter Using Form Pick a student (0-4)! <?php $lastname = array ('Smith', 'Kim', 'Davis', 'Miller', 'Taylor'); $firstname = array('John', 'Anthony', 'Richard', 'Thomas', 'Jessica'); if (!empty($_POST['studentID'])){ $v = $_POST['studentID']; echo "Today's Winner is: ".$firstname[$v]." ".$lastname[$v]; } ?> IST2106 Generate the form
Input Parameter Using Form IST2107 Pick a student (0-4)! <?php $lastname = array ('Smith', 'Kim', 'Davis', 'Miller', 'Taylor'); $firstname = array('John', 'Anthony', 'Richard', 'Thomas', 'Jessica'); if (!empty($_POST['studentID'])){ $v = $_POST['studentID']; echo "Today's Winner is: ".$firstname[$v]." ".$lastname[$v]; } ?> The value input into the textbox will be assigned to variable studentID and will be stored in POST container. In PHP segment, we can retrieve the value this variable using $_POST[‘studentID’]. Note, the variable name should be exactly the same (case sensitive)!
Input Parameter Using Form IST2108 Pick a student (0-4)! <?php $lastname = array ('Smith', 'Kim', 'Davis', 'Miller', 'Taylor'); $firstname = array('John', 'Anthony', 'Richard', 'Thomas', 'Jessica'); if (!empty($_POST['studentID'])){ $v = $_POST['studentID']; echo "Today's Winner is: ".$firstname[$v]." ".$lastname[$v]; } ?> We retrieve the value and assign to variable $v. Finally, output the student name with index $v.
Overview of the whole process Request: date_form.php Form submitted to: Date_form.php IST2109 Webspace User
Try it yourself IST21010 Pick a student (0-4)! <?php $lastname = array ('Smith', 'Kim', 'Davis', 'Miller', 'Taylor'); $firstname = array('John', 'Anthony', 'Richard', 'Thomas', 'Jessica'); if (!empty($_POST['studentID'])){ $v = $_POST['studentID']; echo "Today's Winner is: ".$firstname[$v]." ".$lastname[$v]; } ?> lottery2.php
A More General Case IST Input form in one page; PHP response in another page
IST21012 Pick a student (0-4)! <?php $lastname = array ('Smith', 'Kim', 'Davis', 'Miller', 'Taylor'); $firstname = array('John', 'Anthony', 'Richard', 'Thomas', 'Jessica'); if (!empty($_POST['studentID'])){ $v = $_POST['studentID']; echo "Today's Winner is: ".$firstname[$v]." ".$lastname[$v]; } ?> Pick a student (0-4)! lottery3.php <?php $lastname = array ('Smith', 'Kim', 'Davis', 'Miller', 'Taylor'); $firstname = array('John', 'Anthony', 'Richard', 'Thomas', 'Jessica'); $v = $_POST['studentID']; echo "Today's Winner is: ".$firstname[$v]." ".$lastname[$v]; ?> process_lottery3.php lottery2.php Separate into a form page and a process page
Overview of the whole process Request: date_form.php Form submission to: Date_form.php IST21013 Webspace User
IST21014 So what happens if I directly visit The variables do not have values
Try it yourself IST21015 Pick a student (0-4)! lottery3.php <?php $lastname = array ('Smith', 'Kim', 'Davis', 'Miller', 'Taylor'); $firstname = array('John', 'Anthony', 'Richard', 'Thomas', 'Jessica'); $v = $_POST['studentID']; echo "Today's Winner is: ".$firstname[$v]." ".$lastname[$v]; ?> process_lottery3.php
In-class Exercise 1.Create yourname.php to take a user’s name and favorite number as input 2.Create process_yourname.php to process the user input and display a welcome message
Forms Forms enable server-user interaction –Allow users to input data –User inputs sent to a server program –The server program processes the query Examples: –User login –User registration –Search Usually three steps –Input page, visual part of the form, coded in HTML –Form validation, a program to check the data before it enters the server (often JavaScript) –Form processing, a program that accepts and processes the input IST21017
Forms Formal definition –A form is an area that can contain data input elements Text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc Defined with the tag and a set of attributes –name: the name of the form –action: the program that will processes the form –method: how this form will be submitted IST21018
Form Definition This line says: –The form will be processed by “process_lottery3.php” –By clicking “go”, a user sends an HTTP request to “process_lottery3.php” –All the form data is attached to the HTTP request via “post” method You can also use “get” method For difference between “post” and “get”, see IST21019
Form Elements The most used form element tag is the tag –The type of input is specified with the type attribute Text, Dropdown List, Radio button, Checkbox, Submit –The name attribute is usually used by validation or processing code to retrieve the input data. In most cases, it has nothing to do with displaying the element IST
Form Elements: Text Fields Text fields are used when you want the user to type letters, numbers, etc. in a form Use value attribute to preset some text in the field IST21021
Form Elements: Submit Button Submit button –When the user clicks the "Submit" button, the content of the form is sent to another file, which is defined by action attribute of the tag IST21022
Form Elements: Radio Buttons Radio Buttons are used when you want the user to select one of a limited number of choices IST21023 Pick a student (0-4)! Value that will be passed to the process pageValue show on the page
Form Elements: Checkboxes Checkboxes are used when you want the user to select one or more options of a limited number of choices. IST21024 Pick a student (0-4)!
Form Elements: Dropdown List Let the user choose from a set of given options. IST Pick a student (0-4)!
More about forms