PHP Form Handling

0
2538

Today we will learn to take input dynamically from a user through HTML forms in this PHP Form Handling tutorial.

PHP is a scripting language mostly used to create websites. It requires HTML form to accept input dynamically from user. So we use HTML forms to get input from user. Any form element in HTML is automatically available to PHP scripts. The data in the form is stored in the superglobal array variables like $_GET or $_POST.
These are the global array variables that can be accessed anywhere in the script, may it be a function, a class or a file without doing any special task. Hence called superglobals. As they are array variables, they can store multiple values in them.
Let us have a step by step look on how to take input from a user:

  • Accessing input using $_POST method:
    1. Creating a HTML form:
      • Let us create a HTML form in index.html page.
      • So create a new folder named Forms in the htdocs folder in xampp folder in C drive. Open a new notepad++ document and save it as index.html in the newly created Forms folder.
      • Write the following code in index.html page:
      • In the above code we have created a form with heading “Company Registration”.
      • The form contains text input fields to accept the details required for registering a company such as company name, owner/proprietor name, PAN card no., name on PAN card and mobile number.
      • A submit button with caption SUBMIT is present to take the input to desired location.
      • The form tag has value post in its method attribute. The method post is used to carry the input data to the specified URL by hiding it from the outside world. It can carry a large amount of data.
      • Next, form tag has one more attribute called action that has register.php value in it.
      • This register.php is the PHP page where the input accepted from the form will be carried in the $_POST array superglobal variable.
      • These superglobal array variables store values in the form of key/value pairs as shown below:
      • When you click on the SUBMIT button, you will be redirected to register.php page.
    2. Creating a PHP file:
      • Let us now write the code for register.php page which is saved in the Forms folder itself.
      • The code is given below:
      • We know that PHP pages can contain HTML as well as PHP code.
      • In the above code we are accessing the data entered by the user in the form in index.html page.
      • The data is accessed using $_POST superglobal variable using the name given to the input field.
      • The establishment name is accessed by using the value of the name attribute of the input element used to accept establishment name from the user as the key in the superglobal $_POST variable.
      • It is shown as follows: $_POST[“estname”]; here estname is the name of the input element used to accept the establishment name.
      • Similar all the input data can be accessed.
      • Here we are using $_POST array superglobal variable because, the method used by the form is post. If we use get method in the form then we will have to use $_GET array superglobal variable.
      • The output of HTML form is shown first:
      • post_form_1
        fig 1

      • When we click on the SUBMIT button, we get the following output:
      • post_form_2

      • fig 2
  • Accessing input using $_GET method:
    1. Creating a HTML form:
      • Write the following code in index.html page:
      • Here we have accepted the user name and age to test if he is eligible for voting.
      • The form has name form1 and method as get. The data when transferred using get method in a form is visible in the URL of the page in the address bar.
      • Hence the get method is never used to transfer confidential data such as password. Moreover, when get method is used in a form it can carry only that much data that fits in the address bar.
      • The action attribute of form has value eligible.php. This is a PHP page that processes the input data.
      • The data accepted from the user in the form can be accessed in the eleigible.php page using $_GET superglobal variable.
    2. Creating a PHP file:
      • Let us write a PHP code to process the input accepted from user.
      • The code is given below:
      • Here, we have taken a decision using if…else statement by checking whether the candidate is above or below 18 years of age.
      • If the candidate is above or equal to 18 years the statements of if block are displayed indicating that the candidate is eligible for voting.
      • If the candidate is below 18 years the statements of else block are displayed indicating that the candidate is not eligible for voting.
      • Here the user input is accessed using $_GET superglobal array variable.
      • The name of the input element is used as the key and its value is accessed.
      • It is used as shown below:
      • $_GET[“name”]; here name is the name of the input field name used to accept user name.
      • The output of the form is shown below:
      • get_form_1.jpg
        fig 3

      • Now when you click on the Test button, the following output will appear.
      • get_form_2
        fig 4

      • Remember that you have to use $_POST superglobal array variable to access the user input, when you use the post method in the form.
      • And you have to use $_GET superglobal array variable to access the user input, when you use the get method in the form.
      • In the above forms you can notice that you can enter letters in the text fields given for mobile number and age. This can be avoided using validations for the form elements. We will see it in next session.

Thus we learned how to accept input from the user in HTML forms and how to process it in PHP.

LEAVE A REPLY

Please enter your comment!
Please enter your name here