A form is a document containing blank fields that a user can fill the data or the user can select the data. The data will get stored 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 them to the web server for further processing.
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 come into the picture when developing flexible and dynamic applications that accept user input.
- Forms are useful to edit already existing data in the database.
Creating a Simple form
The PHP contains two superglobals $_GET and $_POST for form-data processing.
Follow the steps given below 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 a built-in PHP superglobal array variable that is useful to get values submitted via the 5 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 the 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 on the length of the values since they are submitted via the body of the HTTP||This method has a restriction on the size of the values, usually 255 characters.|
|The POST has a lower performance as compared to the 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
PHP form Handling is an important concept, especially for backend development. We hope you have gained a complete understanding of how forms work and how to create them.
If you want to understand the other concepts of PHP programming, do refer to the PHP Tutorial and PHP MySQL sections. Happy Learning 🙂