Today we will learn about Form In PHP. It is used to create dynamic forms like email address forms, registration forms, etc. Before these, we also discussed PHP – Loops | for-loop, while-loop, do-while loop PHP Control Structures: If else, Switch Case, Data Type in PHP:, Operators In PHP, if you don’t know then Hurry up Now!
Introduction to PHP-Form
To create a form, <form> element is used.
<form action="process.php" method="post">
</form>
The <form> element has two essential attributes:
action: It determines the URL that processes the form submission. In this example, the process.php will process the form.
method: It determines the HTTP method for submitting the form.in PHP the most commonly used form methods are POST and GET. In this below example, the form method is post.
Remember that the form method is case-insensitive. It means that we can use either post or POST.
PHP-Form Handling
To collect form data in PHP we used superglobals that are $_GET and $_POST.
HTTP POST Method:
The web browser will include the form data in the HTTP request’s body if a form uses the POST method. After submitting the form, we can access the data via the associative array i.e $_POST in PHP.
Post request is widely used to submit forms that have a large amount of data such as file upload, image upload, login form, registration form, etc.
It has the following syntax:
<?php
$_POST['variable_name'];
?>
HTTP GET Method:
When we submit a form using the GET method, we can access the data from the form via the associative array i.e $_GET.
In PHP GET request is the default form request. it is not secured because the data passed through GET request is visual on the URL browser. You can send a limited amount of data through GET requests.
It has the following syntax:
<?php
$_GET['variable_name'];
?>
Let we take an example:
<form action="welcome.php" method="get">
Name: <input type="text" name="name"/>
<input type="submit" value="visit"/>
</form>
File: welcome.php
<?php
$name=$_GET["name"]; //receiving name field value in $name variable
echo "Welcome, $name";
?>
When and why we are using forms in HTML?
- Forms in PHP come in handy when developing flexible and dynamic applications that accept user input.
- We can use Forms to edit already existing data from the database.
PHP – A Simple HTML Form
Let us take an example i.e a simple HTML form with two input fields and a submit button:
<!DOCTYPE HTML>
<html>
<body>
<form action="process.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="button" value="Confirm">
</form>
</body>
</html>
When a user fills out the form above and clicks the Confirm button, the form data is sent for processing to a PHP file named “process.php”. The data are in the form that is sent with the HTTP POST method.
To display the submitted data you could echo all the variables. The “process.php” looks like this:
<html>
<body>
Welcome: <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
The output could be like this: