Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Forms. I. Using PHP with HTML Forms A very common application of PHP is to have an HTML form gather information from a website's visitor and then.

Similar presentations


Presentation on theme: "PHP Forms. I. Using PHP with HTML Forms A very common application of PHP is to have an HTML form gather information from a website's visitor and then."— Presentation transcript:

1 PHP Forms

2 I. Using PHP with HTML Forms A very common application of PHP is to have an HTML form gather information from a website's visitor and then use PHP to do process that information. In this lesson we will simulate a small business's website that is implementing a very simple order form. Imagine we are an art supply store that sells brushes, paint, and erasers. To gather order information from our prospective customers we will have to make a page with an HTML form to gather the customer's order. Note: This is an oversimplified example to educate you how to use PHP to process HTML form information. This example is not intended nor advised to be used on a real business website.

3 The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts. We first create an HTML form that will let our customer choose what they would like to purchase. This file should be saved as "order.html" Paint Brushes Erasers Quantity:

4 A form can have the method set as post or get. When using a form with method = "post" you can use $_POST to access the form values. And when the form is using method = "get" you can use $_GET to access the values. The $_REQUEST super global can be used to access form values with method="post" and method="get" but it is recommended to use $_POST or $_GET instead so you will know from what method did the values come from. Next we must alter our HTML form to specify the PHP page we wish to send this information to. Also, we set the method to "post".

5 order.html Code adding post : Jamuna Art Supply Order Form Paint Brushes Erasers Quantity: Now that our "order.html" is complete, let us continue on and create the "process.php" file which will process the HTML form information.

6 We want to get the "item" and "quantity" inputs that we have specified in our HTML form. The proper way to get this information would be to create two new variables, $item and $quantity and set them equal to the values that have been "posted". The name of this file is "process.php". <?php $quantity = $_POST['quantity']; $item = $_POST['item']; echo "You ordered ". $quantity. " ". $item. ". "; echo "Thank you for ordering from Jamuna Art Supplies!"; ?>

7 1. The GET method The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.  The GET method produces a long string that appears in your server logs, in the browser's Location: box.  The GET method is restricted to send up to 1024 characters only.  Never use GET method if you have password or other sensitive information to be sent to the server.  GET can't be used to send binary data, like images or word documents, to the server. csc http://www.test.com/index.htm?name1=value1&name2=value2

8  The data sent by GET method can be accessed using QUERY_STRING environment variable.  The PHP provides $_GET associative array to access all the sent information using GET method. As we mentioned before, the alternative to the post method is get. If we were to change our HTML form to the get method, it would look like this:...

9 Example : Get_method.php Using Method Get " method="get"> Using Get method Name: Age: <?php if($_GET['name'] !="" || $_GET['age']!="" ) { echo "Welcome ". $_GET['name']. " "; echo "You are ". $_GET['age']. " years old."; exit(); } ?>

10  There are a few disadvantages to passing variables through a URL: Everyone can see the values of the variables, so passing sensitive information isn’t really very secure using this method. The user can change the variable value in the URL, leaving your site potentially open to showing something you’d rather not show. A user might also pull up inaccurate or old information using a saved URL with older variables embedded in it.

11 When to use method="get"? When using method="get" in HTML forms, all variable names and values are displayed in the URL. Note: This method should not be used when sending passwords or other sensitive information! However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases. Note: The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.

12 2. The POST Method The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.  The POST method does not have any restriction on data size to be sent.  The POST method can be used to send ASCII as well as binary data.  The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.  The PHP provides $_POST associative array to access all the sent information using GET method.

13 Example : Post_method.php <?php if( $_POST["name"] || $_POST["age"] ) { echo "Welcome ". $_POST['name']. " "; echo "You are ". $_POST['age']. " years old."; exit(); } ?> " method="POST"> Name: Age:

14 When to use 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. However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

15 3. The $_REQUEST variable The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods. Example : Welcome ! You are years old.

16 Example : request.php <?php if( $_REQUEST["name"] || $_REQUEST["age"] ) { echo "Welcome ". $_REQUEST['name']. " "; echo "You are ". $_REQUEST['age']. " years old."; exit(); } ?> " method="POST"> Name: Age:

17 " method="post">

18 THE END


Download ppt "PHP Forms. I. Using PHP with HTML Forms A very common application of PHP is to have an HTML form gather information from a website's visitor and then."

Similar presentations


Ads by Google