Submission of an HTML Form
Pre-requisites: HTML, PHP
In this project:
- We will create a simple HTML form.
- Submit the form information to the web server using PHP.
- Differentiation between GET & POST submission methods.
- Display the submitted information on screen to the user.
Type the following code in an editor and save the file as UserForm.php in your root directory.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Form</title>
</head>
<body>
<form action="process.php" method="POST">
<label>Name<sup>*</sup></label>
<input type="text" name="name" />
<label>Email<sup>*</sup></label>
<input type="text" name="email" />
<label>Gender<sup>*</sup></label>
<input type="radio" name="gender" value="female" checked="checked" /> Female
<input type="radio" name="gender" value="male" /> Male
<label>Reviews</label>
<textarea rows="3" cols="15" name="reviews"></textarea>
<input type="submit" name="submit" value="SUBMIT">
</form>
</body>
</html>
http://localhost/userForm.php
Run userForm.php file in your browser to get the following output. URL may look like as follows :
OUTPUT:
Code Explanation of UserForm.php
-
<form action="process.php" method="POST">
- On form submission, the form data will be sent to the
process.php
file for further processing as mentioned in the action attribute of form tag. - Form data is sent with the HTTP POST method as mentioned in the method attribute.
- Form can be submitted using HTTP GET method. Use
method=”GET”
instead ofmethod=”POST”
in the form tag.
METHOD |
DESCRIPTION |
|
$_GET |
Values are sent to the server via URL parameters. URL may look like :
|
|
$_POST |
Values are sent to the server via HTTP Post method and values are not visible in the URL. URL may look like :
|
So after submitting data from UserForm.php
file we need to display those submitted data so now we’re creating a new file Process.php to display that submitted data.
For example, we can simply display those values as mentioned below:
Output
Code Explanation of Process.php
-
<h4>Welcome <?php echo $_POST["name"]; ?></h4>
Where, $_POST
is the superglobal PHP array and name is the “name”
attribute of
HTML form tag. Other values can also be accessed in a similar way.
Summary
In this example we have learned how user first fill data in UserForm.php
file and get to know what important elements we have to enter in the form to get in action like the most important tag is <form>…</form>
tag, in this tag all other <input />
tags are binded to get the input from the user, action and method attributes are the most important attributes of <form>
tag without which your data may not submit properly.
In Process.php file we’re display the data i.e. submitted from the UserForm.php
file as there is also a possibility that we can show the data on the same page without creating this new file (Process.php), so in that case we need to place display code in the same UserForm.php
file and in the <form>
tag we need to mention the blank action attribute in that.