A Form is a document containing blank fields that the user can fill the data or user can select the data. The data will store in the database. In this unit, we learn about PHP form handling.
What is a form in PHP?
When we login into a website or mailbox, we are interacting with a form. Forms are useful to get input from the user and submit it to the webserver for further process.
The figure below shows the complete form handling process.
A form contains GUI items such as input box, checkboxes radio buttons etc.
The form contains the <form>…</form> tags and GUI items are specified using form elements such as input.
When and why we use form tag in PHP?
- Forms comes in picture when developing flexible and dynamic applications that accept user input.
- Forms are useful to edit already existing data from the database.
Creating a Simple form
The PHP contains two superglobals $_GET and $_POST for form-data processing.
Below is the list that we need to create a form.
- The Opening and closing form tags <form>…</form>
- The Form submission type POST or GET
- The Submission URL that will process the submitted data
- The Input fields such as input boxes, text areas, buttons, checkboxes etc.
Simple PHP Registration Form
<html> <head> <title>Registration Form</title> </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="hidden" name="form_submitted" value="1" /> <input type="submit" value="Submit"> </form> </body> </html>
- The <form…>…</form> represents the Opening and closing tags.
- The action=”registration_form.php” method=”POST”> represents the destination URL and the submission type.
- First/Last name represents the labels for the input boxes.
- The <input type=”text”…> represents the input box tags
- <br> specifies the new line tag.
- The <input type=”hidden” name=”form_submitted” value=”1″/> represents a hidden value.
- The <input type=”submit” value=”Submit”> specifies the button that when clicked submits the form to the server for processing.
Submitting the Form data
The action attribute specifies the URL that processes the data. The method attribute specifies the submission type.
- The Post method is the built-in PHP superglobal array variable that is useful to get values submitted via HTTP POST method.
- The Post method is useful when we do not want to display the form post values in the URL.
- An excellent example of using post method is when submitting login details to the server.
<?php $_POST['variable_name']; ?>
- “$_POST[…]” is the PHP array
- “’variable_name’” is the URL variable name.
- The Get method is a built-in PHP superglobal array variable that is useful to get values submitted via the HTTP GET method.
- The array variable can be easily accessed from any script in the program; it has a global scope.
- The Get method displays the form values in the URL.
- It is useful for search engine forms as it allows the users to bookmark the results.
<?php $_GET['variable_name']; ?>
- “$_GET[…]” is the PHP array
- “’variable_name’” is the URL variable name.
Difference between Get and Post Method
|The Values are not visible in the URL||The Values are visible in the URL|
|This method does not have any limitation of the length of the values since they are submitted via the body of HTTP||This method has a restriction on the size of the values, usually 255 characters.|
|The Post has lower performance compared to Php_GET method.||The Get has high performance compared to the POST method.|
|It Supports different data types such as string, numeric, binary etc.||It Supports only string data types because the values are displayed in the URL|
|Results cannot be bookmarked.||Results can be bookmarked due to the visibility of the values in the URL|
Processing of registration form data
The registration form submits the data as specified in the action attribute.
Once a form is submitted, the values are populated in the $_POST superglobal array.
We use the PHP isset function to check if the form values have been filled in the $_POST array and process the data.
<html> <head> <title>Registration Form</title> </head> <body> <?php if (isset($_POST['form_submitted'])): ?> //this code is executed when the form is submitted <h2>Thank You <?php echo $_POST['firstname']; ?> </h2> <p>You have been registered as <?php echo $_POST['firstname'] . ' ' . $_POST['lastname']; ?> </p> <p>Go <a href="/registration_form.php">back</a> to the form</p> <?php else: ?> <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="hidden" name="form_submitted" value="1" /> <input type="submit" value="Submit"> </form> <?php endif; ? > </body> </html>
- <?php if (isset($_POST[‘form_submitted’])): ?> it checks if the form_submitted hidden field has been filled in the $_POST array and display a thank you and first name message.
- If the form_submitted field hasn’t been filled in the $_POST array, the form is displayed.
Read More: PHP Form Validations
I hope you have an understanding of PHP form handling.