Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.

Similar presentations


Presentation on theme: "Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms."— Presentation transcript:

1 Lecture 6/2/12

2 Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts. 2

3 $_POST The $_POST variable is an array of variable names and values sent by the HTTP POST method The $_POST variable is used to collect values from a form with method="post“ Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send 3

4 Example Untitled 1 Name: Age: 4

5 Example Continued.. Untitled 1 Welcome. You are years old 5

6 Why use POST? Variables sent with HTTP POST are not shown in the URL Variables have no length limit 6

7 $_REQUEST The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods 7

8 $_GET The $_GET variable is an array of variable names and values sent by the HTTP GET method The $_GET variable is used to collect values from a form with method="get" Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and it has limits on the amount of information to send (max. 100 characters) 8

9 Get Example Untitled 1 Name: Age: 9

10 Get Continued… Grab form values Welcome. You are years old 10

11 Form Validation Client side validation is faster, and will reduce server load However, any site that gets enough traffic to worry about server resources, may also need to worry about site security Use server side validation if the form accesses a database A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page The user will then get the error messages on the same page as the form This makes it easier to discover the error 11

12 Multiple Form Web Sessions Leads the user through a series Of HTML forms that work together to pass data from form to form E.g. shopping cart and multi-page web form 12

13 Using Hidden Fields HTML form element that the browser does not display The receiving PHP script can retrieve any variable name and value defined in a hidden field form element like any other HTML form element 13

14 Note A hidden field is not completely invisible from the user – they can be viewed by looking at the HTML source Tip: You shouldn’t store any data in a hidden field that you don’t want the user to view 14

15 To do.. Create a normal form as our first page The first form needs nothing special as it is only passing its data to the next page like any other form It does not need to worry about accepting data from a previous page 15

16 Form 1 Name: 16

17 Form 2 Unlike the first, this form needs to take the data from a previous form and be able to pass it along Take each piece of data from the previous form and placing it in a hidden field While in our example we are only dealing with two pages, this second form could be a template for any number of pages in between the start and finish of a multi-page form 17

18 Form 2 Takes the specific values out of the $_POST array and stores them in a more manageable variable name Then when actually creating the form, place those values in a hidden field so that they are passed on to the next page. 18

19 Multi-page Form - Page Two Please fill in the following information Address: Phone: " /> 19

20 Final Form Lastly, we will have a script that will process the data submitted to it This form is identical to any script you would normally use to process form data It accepts data posted to it and then processes it Stores each of the passed $_POST variables into an easier to use variable name and then displays the data that the user has entered. 20

21 Multi-page Form - Final You filled in: Name: Address: Phone: 21


Download ppt "Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms."

Similar presentations


Ads by Google