Learn How To Handle Form In PHP

admin_img Posted By Bajarangi soft , Posted On 06-10-2022

Forms are used to get input from the user and submit it to the web server for processing.

Form Handling In PHP

Why we are using form ?

Forms are used to get input from the user and submit it to the web server for processing.
A form is an HTML tag that contains graphical user interface items such as input box, check boxes radio buttons etc.
Forms come in handy when developing flexible and dynamic applications that accept user input.
Forms can be used to edit already existing data from the database.

How To Create A Form ?

We will use HTML tags to create a form. Below is the minimal list of things you need to create a form.
  1. Opening and closing form tags <form>…</form>.
  2. Form submission type POST or GET.
  3. Submission URL that will process the submitted data.
  4. Input fields such as input boxes, text areas, buttons,checkboxes etc.

A Simple Registration Form using POST method

<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <h2>Registration Form</h2>
    <form action="registration_form.php" method="POST"> First name:
        <input type="text" name="firstname"> <br> Last name:
        <input type="text" name="lastname">
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Here, you can the action attribute in  form tag that  defines the action to be performed when the form is submitted.
Usually, the form data is sent to a file on the server when the user clicks on the submit button. In the example , the form data is sent to a file called "registration_form.php".
In this is the built in PHP super global array variable that is used to get values submitted via HTTP POST method.
Post method used array variable that can be accessed from any script in the program.
if Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.

A Simple Registration Form using GET method

<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <h2>Registration Form</h2>
    <form action="registration_form.php" method="GET"> First name:
        <input type="text" name="firstname"> <br> Last name:
        <input type="text" name="lastname">
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Get request is the default form request.The data passed through get request is visible on the URL browser so it is not secured. You can send limited amount of data through get request.
Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL).
GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

 

Related Post