Web Programming TutorialsPHP Form Handling

PHP Form Handling

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:
      • <!DOCTYPE html>
        <html>
        <head>
        <title>PHP Form Handling</title>
        <style>
        	h1{
        		color:purple;
        		margin-left:10px;		
        	}
        	
        	label{
        	
        		display:inline-block;
        		width:150px;
        		margin-left:10px;
        	}
        	
        	.space{
        		color:purple;
        		font-weight:bold;
        		padding:10px;
        		margin-top:10px;
        		margin-left:120px;
        	}
        	
        </style>
        </head>
        <body>
        <h1>Company Registration</h1>
        <form name="register" method="post" action="register.php">
        <label for="estname">Establishment Name:</label>
        <input type="text" name="estname"/><br>
        <label for="propname">Proprietor Name:</label>
        <input type="text" name="propname"/><br>
        <label for="panno">PAN Card Number:</label>
        <input type="text" name="panno"/><br>
        <label for="panname">Name on PAN Card:</label>
        <input type="text" name="panname"/><br>
        <label for="mob">Mobile Number:</label>
        <input type="text" name="mob"/><br>
        <input class="space" type="submit" value="SUBMIT"/>
        </form>
        </body>
        </html>
        
      • 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:
      • array(key1”=>”value1”, “key2”=>”value2”,”key3”=>”value3”,…);
      • 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:
      • <html>
        <head>
        <title>Registration Successful</title>
        <style>
        	h1{
        		color:purple;
        	}
        </style>
        </head>
        <body>
        <?php
        echo "<h1>Your establishment has been registered successfully!</h1>";
        echo "The details are as follows:<br><br>";
        ?>
        <b>Establishment Name:</b> <?php echo  $_POST["estname"];?><br>
        <b>Proprietor Name:</b> <?php echo  $_POST["propname"];?><br>
        <b>PAN Card Number:</b> <?php echo  $_POST["panno"];?><br>
        <b>PAN Card Name:</b> <?php echo  $_POST["panname"];?><br>
        <b>Mobile Number:</b> <?php echo  $_POST["mob"];?> 
        </body>
        </html>
        
      • 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:
      • <!DOCTYPE html>
        <html>
        <head>
        <title>PHP Form Handling</title>
        <style>
        	h1{
        		color:purple;
        		margin-left:10px;		
        	}
        	
        	label{
        	
        		display:inline-block;
        		width:150px;
        		margin-left:10px;
        	}
        	
        	.space{
        		color:purple;
        		font-weight:bold;
        		padding:10px;
        		margin-top:10px;
        		margin-left:120px;
        	}
        	
        </style>
        </head>
        <body>
        <h1>Election Eligibility Test</h1>
        <form name="form1" method="get" action="eligible.php">
        <label for="name">Enter your name:</label>
        <input type="text" name="name"/><br>
        <label for="age">Enter your age:</label>
        <input type="text" name="age"/><br>
        <input class="space" type="submit" value="Test"/>
        </form>
        </body>
        </html>
      • 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:
      • <html>
        <head>
        <title>Registration Successful</title>
        <style>
        	h1,h2{
        		color:purple;
        	}
        </style>
        </head>
        <body>
        <?php
        	if($_GET["age"]>=18)
        	{
        		echo "<h1>".$_GET["name"]."</h1>";
        		echo "<h2>You are eligible for voting.</h2>";
        	}
        	else
        	{
        		echo "<h1>".$_GET["name"]."</h1>";
                echo "<h2>You are not eligible for voting.</h2>";
        	}
        ?> 
        </body>
        </html>
        
      • 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.

Previous articlePHP Form Validation
Next articleError Handling in PHP

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -