Form Handling in PHP language
PHP is a powerful language for making dynamic and interactive websites. In this exercise we will use HTML and PHP languages to build a form for a website which will get input data from our form and POST it on another page by submitting it. In PHP there are two superglobals $_POST and $_GET which are used to collect data from a form.
By following the steps bellow we will be able to create a simple HTML form and POST it dynamically with the help of PHP language.
Create a new PHP page in Dreamweaver that is a web designing software and create a simple HTML form and create 3 input boxes for the following fields; First Name, Last Name and Age. The code is shown in step 2.
Give your form ‘action’ and ‘method’ attributes. Form Action attribute is used to define where the form will POST and in Form Method attribute we will define if the form method will be ‘GET’ or ‘POST’. In this exercise we will use POST method because we will be posting our data to another page that is named ‘post.php’. In the Action field type the URL of the page where you are redirecting the user, in this exercise that will be the post.php file. We have given names to all our input boxes which will help us to find the values of these input boxes.
Create another PHP page in the same folder where our index.php file is and save it with the name of post.php this name has been used in the Action attribute in the form of index.php page.
Type the following code in post.php page and this will echo (print) the values of the input boxes of the form of index.php page and display it on post.php page.
Simple! we store the data in $_POST and print it using echo.