Wednesday, 1 June 2011

HOW TO USE HTML FORMS IN PHP


 - WHY WE NEED HTML FORMS



HTML forms are there to help you process the inputs given by your visitors in order to allow for more interactivity; for instance, if you plan to build an auction website or a dating website, you need to retrieve information about your users in order to set up their accounts. In order to obtain such information, you can set up HTML forms (i.e. interfaces) which invite the user to enter the required information. This data will then be stored within PHP variables (the nature of which will be discussed later) and you will be able to manipulate them within your PHP scripts.

HOW TO USE HTML FORMS IN PHP - CREATION OF AN HTML FORM



The creation of an HTML form is done according to the following syntax:

Learn the HTML code:
<form action="target.php" method="get">
Income: <input type="text" name="your_income" />
Debt: <input type="text" name="your_debts" />
<input type="submit" />
</form>
  • where target.php is the file that your visitor will be redirected to once he has submitted the information by clicking the button (file from where you will be able to proceed the inputs that were submitted to you).
  • where your_income and your_debts are the names of the inputs (names are used to be able to identify these inputs later on in PHP; we will see how a few lines below). Of course, there is no restriction on the number of such inputs (i.e. you could perfectly decide to integrate an arbitrary number of input fields).
  • where input type="submit" designates a submit button (clicking this button will trigger the sending of the information of the PHP form and redirect the visitor to the page target.php where actions will be taken depending upon the information that was retrieved from the HTML form.
  • where method="get" indicates which method will be used by the HTML form in order to transmit the information from your visitor's browser to your web server. There exist two different methods to achieve this; each of them will be developped below.


Remark:

Please note that HTML forms are indeed HTML code; in this way, you must ascertain that the code is typed in HTML mode (that is to say, it won't work if you type it in PHP mode, i.e. under the tags <?php and ?>). However, the information retrieved from these forms will be processed in PHP (as you will see below).

HOW TO USE HTML FORMS IN PHP - PROCESSING THE INPUTS FROM THE HTML FORM TO THE WEB SERVER USING THE GET METHOD



Transmission of the user input via URL

The GET method is the one which was used in the previous example of an HTML form. Practically, the GET method passes on the information of the HTML form through the URL; in the example above, let's see what happens when the information is transmitted by the web browser after clicking the HTML button:

Run the HTML script in your web browser (before clicking the HTML form):

HTML forms and the GET method
Run the HTML script in your web browser (after clicking the HTML form):

HTML forms and the GET method


Remark:

Upon clicking the submit button, you are indeed redirected to the target.php file, but the URL of that page is somehow longer than it would usually be.

Once the form has been submitted, the information (the inputs) that are carried by this form are passed in the URL under the form that you can observe:

normal_url?name1=value1&name2=value2&...
  • where normal_url designates the URL of the target webpage (that which was indicated at the creation of the HTML form).
  • where the question mark ? is appended at the end of the URL and marks the fact that the user inputs from the HTML form will be passed in the coming variables.
  • where namei designates the names of the inputs, as determined at the creation of the HTML form.

Retrieving the inputs from the HTLM form

Retrieving those inputs in your PHP scripts is easy: they can be accessed via the variable $_GET (which is a superglobal PHP array (a predefined reserved PHP variable). Thus, $_GET['your_income'] will return the input as given by the HTML form:

Learn the PHP code (code contained within target.php):
<?php echo $_GET['your_income']; ?>,

Run the PHP script in your web browser (after clicking the submit button on the HTML form):

Using the get method in HTML forms

Remark:

The GET method has a hard limit (namely the maximal number of characters that can be contained within an URL).

HOW TO USE HTML FORMS IN PHP - PROCESSING THE INPUTS FROM THE HTML FORM TO THE WEB SERVER USING THE POST METHOD


Transmission of the user input via the cookies

In order for this method to work, your visitor must have its cookies enabled (more about the manipulation of cookies in PHP will be said in another tutorial). Basically, the syntax remains the same as before except that you will set method="post" instead ofmethod="get" in the definition of the HTML form and that in order to retrieve the inputs of the HTML form, you will use the variable $_POST instead of $_GET.

Remark:

There is no theoretical hard limit for the POST method; by default, a 8Mb limit applies but it can be changed by modifying the post_max_size parameter within the PHP configuration file (php.ini).

HOW TO USE HTML FORMS IN PHP - WHY PREFER THE GET OR THE POST METHOD WHEN HANDLING HTML FORMS ?


Reasons why you might prefer to use the POST method:
  • No hard limit in the number of characters (no hard limit in the size of the inputs that will be passed to the HTML form.
  • You do not want the inputs to be visible in your visitor's browser

Reasons why you might prefer to use the GET method:

  • You know that your visitors does not enable cookies.
  • You want that the inputs your visitor left on the HTLM form appear in the URL of the destination page. One reason for this might be that you want to make it possible for your visitor to bookmark the page and get back to it later; another reason might be that the content of the destination page is going to vary as a function of the information carried by the URL, and you want to be able to index all the different pages in search engines (or at least differentiate between them); such a case would occur when you are extracting data from a database (more about this in our SQL tutorials), the data being extracted depending on the information appended at the end of the URL (creation of dynamic webpages): this is the way web forums display their content (forum posts).
We have seen that HTML forms constitute an interface between your visitors and your server, allowing for the transmission of your visitors' inputs to your PHP scripts (where such inputs can be processed as variables).
There exist two different methods to communicate the information from the clients' web browsers to the server: one of them is the GET method, the other is the POST method. We have seen what are the specifities of these two methods.

No comments:

Post a Comment