PHP Form Handling

Form is very important part of any webpage or website. HTML forms are commonly used in web-based applications to capture user input. Contact forms, login forms, search forms, and registration forms are all instances of forms. Forms are the primary means of communication between the user and the server. The form data is usually forwarded to the server for processing. Validating user input, performing database activities, and providing feedback to the user are all part of the user input processing process. In this article, we will learn about PHP Form Handling and its attributes with the help of some examples.

About PHP Form Handling

When a user submits a form, a server-side script receives the data and processes it. the <form> element is used to create a form, as shown below:

<form action="submit.php" method="post"></form>

Two significant characteristics of the element are:

1. action

It always takes the URL for the form’s submission processing. In this case, the form will be processed by submit.php.

2. method

It always takes the form’s HTTP method of submission. POST and GET are the most commonly used form methods. In the above example, the form method is post.

The form method does not care about case. It signifies that you have the option of using either post or POST. If you don’t specify the method attribute, the get method will be used by default by the form element.

Name, type, and value are the most significant aspects of an input element. In PHP, the name attribute will be always used to obtain the value.

POST Method

When a form is submitted using the POST method, the form data is included in the body of the HTTP request. The form data can be accessed after it has been submitted using PHP’s associative array $_POST.

The data supplied through a post request is encrypted because it is not accessible to the URL browser. With a post request, you can submit a significant amount of data.

Example

if a contact form has an email input element, you may use the $_POST[’email’] function in PHP to get the email value. If the form does not have an email input, there will be no element with the key ’email’ in the $_POST.

Let’s take an example of the form data contains the email:

Make a html file named index.html, and put below code in this file

<form action="submit.php" method="post">
	<div>
		<input name="user_email" type="email" />
	</div>
	<input name="submitbtn" type="submit" value="Submit" />
</form>

Make another file submit.php to take email input field data:

<?php
if( isset( $_POST['user_email']) ) 
{
      echo "User mail is: ". $_POST['user_email'];
}
?>

Get Method

The get request is the most common form request. Because the data supplied through the get request is visible in the URL browser, it isn’t safe. With a get request, you can only submit a certain quantity of data. When you submit a form with the GET method, the associative array $_GET is used to access the form data in PHP.

The GET method appends the form data to the URL that processes the form, unlike the POST method. Assume the form’s processing URL is http://test.com/form.php. When you submit a form with the email support@example.com, you’ll notice that the email value gets attached to the URL as follows:

http://test.com/form.php?email=support%40example.com

Here, we already know that the @ is encoded as %40 in the URL. To check if the form data contains the email:

<?php

if(isset($_GET['email']) {
    // write code after get email
}
?>

Let’s take an example of the form data contains the email:

Make a html file named index.html, and put below code in this file

<form action="submit.php" method="get" >
         <div>
                    <input name="user_email" type="email" />
        </div>
        <input name="submit" type="submit" value="Submit" />
</form>

Make another file submit.php to take email input field data:

<?php 
if( isset( $_GET['user_email'] ) ) 
{       
         echo "User email is:  ".$_GET['user_email'];
}
?>

Also read about PHP 7 New Features

Difference Between Post Method and Get Method

POST MethodGET Method
It useful to send any sensitive information because the information is not visible to anyone.It does not be used to send any sensitive information.
It does not limit the amount of data sent to the server.It used to send limited amount of data to the server.
It can also send binary and ASCII data.It does not use to send binary and ASCII data.
Bookmarking the results is not possible.Bookmarking the results is possible.
The data isn’t saved in the browser’s history. It’s hidden.The data is saved in the browser’s history.
The performance is low because POST cannot decode the data.Because of the simple nature of displaying data, performance is high.

For full example of form handling is below:

index.html
<form action="submit.php" method="post">
	Name: <input name="name" type="text" /><br />
	E-mail: <input name="email" type="text" /><br />
	<input type="submit" value="Submit" />
</form>
submit.php
<?php 
// define variables and set to empty values
$name = $email = "";

if ( !empty( $_POST["name"] ) )
{
	$name = $_POST["name"];
}

if ( !empty( $_POST["email"] ) ) 
{
	$email = $_POST["email"];
}

echo "Your given values are as : \r\n";
echo "Your name is $name. \r\n";
echo "Your email address is $email";
?>

Web developers must be proficient in PHP form handling. This post went over the fundamentals of handling file uploads, validating user input, and building and executing forms. Gaining proficiency in these methods will enable you to develop user-friendly, safe, and reliable online apps. As always, make sure to verify and cleanse user input to safeguard your apps against security flaws.

Categorized in: