The Wayback Machine - https://web.archive.org/web/20240624023857/https://www.geeksforgeeks.org/php-form-processing/
Open In App

PHP Form Processing

Last Updated : 15 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to process form in PHP. HTML forms are used to send the user information to the server and returns the result back to the browser. For example, if you want to get the details of visitors to your website, and send them good thoughts, you can collect the user information by means of form processing. Then, the information can be validated either at the client-side or on the server-side. The final result is sent to the client through the respective web browser. To create a HTML form, form tag should be used.

Attributes of Form Tag:

Attribute Description
name or id It specifies the name of the form and is used to identify individual forms.
action It specifies the location to which the form data has to be sent when the form is submitted.
method It specifies the HTTP method that is to be used when the form is submitted. The possible values are get and post. If get method is used, the form data are visible to the users in the url. Default HTTP method is get.
encType It specifies the encryption type for the form data when the form is submitted.
novalidate It implies the server not to verify the form data when the form is submitted.

Controls used in forms: Form processing contains a set of controls through which the client and server can communicate and share information. The controls used in forms are:

  • Textbox: Textbox allows the user to provide single-line input, which can be used for getting values such as names, search menu and etc.
  • Textarea: Textarea allows the user to provide multi-line input, which can be used for getting values such as an address, message etc.
  • DropDown: Dropdown or combobox allows the user to provide select a value from a list of values.
  • Radio Buttons: Radio buttons allow the user to select only one option from the given set of options.
  • CheckBox: Checkbox allows the user to select multiple options from the set of given options.
  • Buttons: Buttons are the clickable controls that can be used to submit the form.

Creating a simple HTML Form: All the form controls given above is designed by using the input tag based on the type attribute of the tag. In the below script, when the form is submitted, no event handling mechanism is done. Event handling refers to the process done while the form is submitted. These event handling mechanisms can be done by using javaScript or PHP. However, JavaScript provides only client-side validation. Hence, we can use PHP for form processing.

HTML Code:




<!DOCTYPE html>
<html>
  
<head>
    <title>Simple Form Processing</title>
</head>
  
<body>
    <form id="form1" method="post">
        FirstName:
        <input type="text" name="firstname" required/>
        <br>
        <br> 
        LastName
        <input type="text" name="lastname" required/>
        <br>
        <br> 
        Address
        <input type="text" name="address" required/>
        <br>
        <br> 
        Email Address:
        <input type="email" name="emailaddress" required/>
        <br>
        <br> 
        Password:
        <input type="password" name="password" required/>
        <br>
        <br>
        <input type="submit" value="Submit”/>
    </form>
</body>
</html>


Form Validation: Form validation is done to ensure that the user has provided the relevant information. Basic validation can be done using HTML elements. For example, in the above script, the email address text box is having a type value as “email”, which prevents the user from entering the incorrect value for an email. Every form field in the above script is followed by a required attribute, which will intimate the user not to leave any field empty before submitting the form. PHP methods and arrays used in form processing are:

  • isset(): This function is used to determine whether the variable or a form control is having a value or not.
  • $_GET[]: It is used the retrieve the information from the form control through the parameters sent in the URL. It takes the attribute given in the url as the parameter.
  • $_POST[]: It is used the retrieve the information from the form control through the HTTP POST method. IT takes name attribute of corresponding form control as the parameter.
  • $_REQUEST[]: It is used to retrieve an information while using a database.

Form Processing using PHP: Above HTML script is rewritten using the above mentioned functions and array. The rewritten script validates all the form fields and if there are no errors, it displays the received information in a tabular form.

  • Example:




    <?php
    if (isset($_POST['submit']))
    {
        if ((!isset($_POST['firstname'])) || (!isset($_POST['lastname'])) || 
            (!isset($_POST['address'])) || (!isset($_POST['emailaddress'])) || 
            (!isset($_POST['password'])) || (!isset($_POST['gender'])))
        {
            $error = "*" . "Please fill all the required fields";
        }
        else
        {
            $firstname = $_POST['firstname'];
            $lastname = $_POST['lastname'];
            $address = $_POST['address'];
            $emailaddress = $_POST['emailaddress'];
            $password = $_POST['password'];
            $gender = $_POST['gender'];
        }
    }
    ?>
    <html>
      
    <head>
        <title>Simple Form Processing</title>
    </head>
      
    <body>
        <h1>Form Processing using PHP</h1>
        <fieldset>
            <form id="form1" method="post" action="form.php">
                <?php
                    if (isset($_POST['submit']))
                    {
                        if (isset($error))
                        {
                            echo "<p style='color:red;'>" 
                            . $error . "</p>";
                        }
                    }
                    ?>
      
                    FirstName:
                    <input type="text" name="firstname"/> 
                     <span style="color:red;">*</span>
                    <br>
                    <br>
                    Last Name:
                    <input type="text" name="lastname"/>
                      <span style="color:red;">*</span>
                    <br>
                    <br> 
                    Address:
                    <input type="text" name="address"/>
                      <span style="color:red;">*</span>
                    <br>
                    <br> 
                    Email:
                    <input type="email" name="emailaddress"/>
                      <span style="color:red;">*</span>
                    <br>
                    <br> 
                    Password:
                    <input type="password" name="password"/>
                       <span style="color:red;">*</span>
                    <br>
                    <br> 
                    Gender:
                    <input type="radio" 
                           value="Male" 
                           name="gender"> Male
                    <input type="radio" 
                           value="Female"
                           name="gender">Female
                    <br>
                    <br>
                    <input type="submit" value="Submit" name="submit" />
            </form>
        </fieldset>
        <?php
           if(isset($_POST['submit']))
           {
              if(!isset($error))
               {
                       echo"<h1>INPUT RECEIVED</h1><br>";
                       echo "<table border='1'>";
                       echo "<thead>";
                       echo "<th>Parameter</th>";
                       echo "<th>Value</th>";
                       echo "</thead>";
                       echo "<tr>";
                       echo "<td>First Name</td>";
                       echo "<td>".$firstname."</td>";
                       echo "</tr>";
                       echo "<tr>";
                       echo "<td>Last Name</td>";
                       echo "<td>".$lastname."</td>";
                       echo "</tr>";
                       echo "<tr>";
                       echo "<td>Address</td>";
                       echo "<td>".$address."</td>";
                       echo "</tr>";
                       echo "<tr>";
                       echo "<td>Email Address</td>";
                       echo "<td>" .$emailaddress."</td>";
                       echo "</tr>";
                       echo "<tr>";
                       echo "<td>Password</td>";
                       echo "<td>".$password."</td>";
                       echo "</tr>";
                       echo "<tr>";
                       echo "<td>Gender</td>";
                       echo "<td>".$gender."</td>";
                       echo "</tr>";
                       echo "</table>";
            }
          }
        ?>
    </body>
      
    </html>

    
    

  • Output:

Note: When the PHP and HTML are coded in a single file, the file should be saved as PHP. In the form, the value for the action parameter should be a file name.



Previous Article
Next Article

Similar Reads

PHP Image Processing and GD Functions Complete Reference
Image processing and GD functions are used to create and manipulate image files in different image formats including GIF, PNG, JPEG, WBMP, and XPM. PHP can deliver the output image directly to the browser. The image processing and GD functions are used to compile the image functions for this work. It can also require other libraries, depending on t
3 min read
DataTables processing Option
DataTables is jQuery plugin that can be used for adding interactive and advanced controls to HTML tables for the webpage. This also allows the data in the table to be searched, sorted, and filtered according to the needs of the user. The DataTable also exposes a powerful API that can be further used to modify how the data is displayed. The processi
3 min read
HTML | parsing and processing
Before starting with the concept, let's go through the terminology in detail. The word parsing means to divide something into its components and then describe their syntactic roles. The word processing is a familiar word and stands for dealing with something using a standard procedure. Combined these two explain how HTML parser works in generating
5 min read
Node.js Image Upload, Processing and Resizing using Sharp package
Often in our web applications, we need to store multiple forms and formats of images, in the form of a profile picture or an image for a product in an e-commerce prototype. In most of our cases, we need to store images compressed to multiple sizes, retaining the quality. For example, for a product at an e-commerce site, we would require storing 3 v
3 min read
Domain Resolution and URL Processing
A domain name is an address that is used to access any website. Domain Name is unique in nature and it is very easy to remember. By typing the Domain Name user can reach the website whichever he desires. The actual address of any website is very complicated but with the help of Domain Name, this complicated address can be converted into a simplifie
5 min read
How to Narrow a Type of Derived Class Instances while Processing an Array ?
When it comes to JavaScript arrays that contain derived class instances, narrowing down the types is a must to access the specific functions or properties that are unique to those derived classes. Narrowing encompasses an act of specifically type-checking and casting objects explicitly to their derived types. Table of Content Using instanceof Opera
2 min read
Utilizing Web Workers for Background Processing in React Hooks Applications
Using Web Workers for background processing in React Hooks applications offers a powerful solution to handle computationally intensive tasks without blocking the main UI thread. With React Hooks, developers can easily integrate Web Workers into their applications to perform tasks such as data processing, image manipulation, or any other heavy compu
9 min read
XML External Entity (XXE) Processing
XML External Entity (XXE) processing vulnerabilities are security concerns in web applications that handle XML data. They arise when an application parses XML input containing references to external entities without proper validation. These entities can point to external resources like files or URLs, and attackers can exploit them for malicious pur
5 min read
How to submit a form or a part of a form without a page refresh using jQuery ?
In this article, we will learn how to submit a form or a part of a form without a page refresh using JQuery, &amp; will understand its implementation through the example. How to prevent an HTML form to submit? Generally, when we submit a form then we should click a submit button then our page will navigate to another route as it is mentioned in the
3 min read
Design a form component which takes input from its user and displays a form
Design: Here are the things that it should support: Grouping of the fields of the form Validations for fields Submit button For example, the user might want to validate if the input is a valid address and might want this component to have default validations for email, etc. Let's try to create a very basic data model for our component. For our mini
5 min read
three90RightbarBannerImg