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.

 

userForm.php

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

 

  1. <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 of method=”POST” in the form tag.

 

METHOD

DESCRIPTION

$_GET

 

Values are sent to the server via URL parameters.

URL may look like :

 

localhost/userForm.php?name=abc&email=a%40x.g&gender=female&reviews=xyz&submit=submit

 

 

 

 

 

$_POST

 

Values are sent to the server via HTTP Post method and values are not visible in the URL. URL may look like :

 

localhost/userForm.php

 

 

 

 

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:

Process.php

 

Output

 

 

Code Explanation of Process.php

 

  1. <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.

Help Us to Improve our content

Let's Talk

© 2021 Lecturely.com. All rights reserved.

Lecturely.com – Imagined by Paras Jairath, Developed by P3 Multisolutions & Powered by Network Bulls.