A form is an area where user can input data. The place in the form where user input data is called form element. A form element can be a text field, text area field, drop-down list, radio buttons and checkbox. In this tutorial we will apply the knowledge we have gained so far and use it with HTML forms. The best thing to use PHP with HTML is that any elements in HTML forms are directly available in PHP scripts. That means the data entered in HTML forms can be manipulated using PHP scripts.
Creating form
A form is created using <form> tag. To learn more about HTML please refer to HTML tutorials available in net. Let us start with a simple HTML form that we use for user feedback.
Collecting information from HTML form using POST method
As we already mentioned, the best thing to use PHP with HTML is that any elements in HTML forms are directly available in PHP scripts. After filling the form, when we pressed Submit Query button, everything we filled in the form will be available in PHP script.
We can notice that we added action and method in the above code, which tells the server what action is to be taken and what method is to be used. In this example, when we click on Submit Query button the page action.php is called and the method used is post. In order to access the form elements we should write action.php like this. Note that the file name should be action.php and saved in the same directory.
When we fill the form and click the submit button, action.php will be called, the URL will look like http://webanddesigners.com/action.php. Information filled in the form is now available in action.php page. post method use $_POST array to save the form information, the name of the form fields will be the key in the $_POST array.
Collecting information from HTML form using GET method
The following code is exactly same as code in example#2, except that the method used here is GET.
action.php will look like as the following code. Note that when GET method is used the data elements are available in $_GET array. The data elements are accessed in the similar fashion.
When we use POST method the information sent is completely invisible. In addition, there is no restriction on the amount of information to be sent. Up to 8mb of data can be sent using POST method. However, because data and variables are not available in URL, it is not possible to bookmark the page. The URL will look like http://webanddesigners.com/action.php
Bhoj R Dhakal is a programmer. He has few years of experience in software development and maintenance. He has published series of PHP tutorials for webanddesigners.