PHP Form Handling

PHP Form Handling


When the developers reate forms to take input from users, like a Login form or a Registration form.Creating a form on the webpage or web-based application is complete by using HTML.

In PHP submit and handling the form we use two superglobals variables $_GET and $_POST for collecting form-data for processing.

<html>
    <body>
        <form action=”form.php” method=”POST”>
          Name: <input type=”text” name=”name”><br/>
         Email: <input type=”text” name=”email”><br/>
         <input type=”submit”>
      </form>
   </body>
</html>

 

In the code above, we have used the tag to create an HTML form, with input fields for Name and Email along with submitting a button to submit the form-data.

In the tag, we have two attributes, action and method, do you know what they are for?

 

  1. action: Using this attribute, we can specify the name of the file which will collect and handle the form-data. In this above example, we have provided the name of a PHP filei.eform.php in the above example.
  2. Method: This attribute specifies the means of sending the form-data, whether it will be submitted via the POST method or GET method.
PHP Form Handling with POST :When we use POST  the form-data is sent to the server using the HTTP POST method.The POST method is ideal when the user does not want to display the form post values in the URL.A good example of using the HTTP POST Method is when submitting login details to the server.

Output :

PHP Form Handling with GET : The GET method is the built-in PHP superglobal array variable that is used to get values to submit via the HTTP GET method.The GET method displays the form values in the URL..GET method is ideal for search engine forms it allows the users to bookmark the results.

The same result could also be achieved using the HTTP GET method:

Example :

 Output :