Introduction to PHP Form | How to Create a Form in PHP and its Syntax | Examples

 


Introduction to PHP Form

Before understanding what is form in PHP, let’s understand what form is?

The form is a document that contains a couple of blank fields that the user has to fill in the data or the user can select the data. The user’s data is stored in the database with the respective user’s data and can be retrieved anytime and anywhere needed.

👊PHP Form

Form in PHP is similar to the forms that are built using HTML except the syntax used. In PHP, forms use the GET and POST methods to print or retrieve the data entered by the user.

When the user enters all the details required in the form and submits the form using the submit button the form is then further sent for processing and the action is performed on the basis of whatever is mentioned in the action function. The form is then sent for further processing using GET or POST methods whichever is mentioned while designing the form.

👉How to Create a Form in PHP and its Syntax?

Forms are used to get the inputs from the user and process the data into the database or submits the data to the corresponding web server for processing purposes. The form contains the HTML tags which will be having the GUI (Graphical User Interface) such as radio buttons, checkboxes, etc.

These components are used in the form so that the user must feel easy to interact with the GUI/webpage or fill the content of the form. Forms are specially prepared for user-friendly purposes where the user who doesn’t have technical knowledge will explore the form in different ways to use it.

Forms are written inside form tag i.e. <form> and </form>. These tags define that the code for form has started and all the input boxes, checkboxes, radio buttons, etc. can be included inside the form and the form can be closed using </form> tag.

Steps to create a form are as follows:

  • We have to open and close a form inside the HTML tags using <form>………..</form> tags.
  • After the form is written it has to be submitted using either GET or POST methods.
  • If you have to include various attributes like input boxes, checkboxes, radio buttons, etc.
  • The submission of the form will process the data filled by the user and the necessary actions will be done.

Syntax:

<html>
<head>
<title> Sample Form Page </title>
</head>
<body>
<h1> Form Sample </h1>
<form action=”<sample.php>” method=”<GET/POST>” > // Two methods GET or POST method to be chosen
Name: <input type = “text” name=”<name that has to be given>”
<input type=”submit” value=”<what you want to show to user for e.g. submit button”>
</form>
</body>
</html>

In the above program, the syntax has been written for the form elements for the user to fill the details for registration purposes of a name. The user will fill the data in the input box mentioned in the program and the user will click on the submit button to process the data and the action of the form will be performed. In the form action, the PHP file mentioned will have the code to process the data in whatever method mentioned in the form i.e. either GET or POST.

💭Get and Post Methods

The given methods in PHP Form are explained below:

  1. Get Method in PHP

In PHP, a superglobal array is used to get the values that are submitted using the HTML page via get method. It is built-in and has a global scope i.e. anyone can see the data or any script can read the data from the program. This method is used to print the data in the URL that is submitted by the user in the form. It is mainly used in the programs where the data has to be visibly entered by the user for e.g. search engines, websites, bookmarks, etc.

  2. Post Method in PHP

In PHP, a superglobal array built-in method is used to get the values that are submitted using the HTML page via the POST method. It has a global scope i.e. anyone can see the data or any script can read the data from the program. This method is used when the user doesn’t want to display the content entered by him in the form elements. The best example of using this method is when the user is using login details for a particular website/application.

👉Examples of Methods in PHP Form

Here are some examples of Get and Post methods which is given below:

Example #1

Code:

<!DOCTYPE HTML>
<html>
<body>
<form action="abc.php" method="POST">
Name: <input type="text" name="Name"><br>
E-mail: <input type="text" name="Email"><br>
Contact Number: <input type="text" name="Number"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Output:

Method post

Example #2

Code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
$Name = $Email = $Gender = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["Name"]);
$email = test_input($_POST["Email"]);
$gender = test_input($_POST["Gender"]);
}
function test_input($data) {
$data = trim($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h3>FORM IN PHP EXAMPLE</h3>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
Name: <input type="text" name="Name">
<br><br>
E-mail: <input type="text" name="Email">
<br><br>
Gender:
<input type="radio" name="Gender" value="female">Female
<input type="radio" name="Gender" value="male">Male
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Output:

PHP Form eg2

Example #3

Code:

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
$NameError = $EmailError = $GenderError = "";
$Name = $Email = $Gender = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Name"])) {
$NameError = "Name is required";
} else {
$Name = test_input($_POST["Name"]);
}
if (empty($_POST["Email"])) {
$EmailError = "Email is required";
} else {
$Email = test_input($_POST["Email"]);
}
if (empty($_POST["Gender"])) {
$GenderError = "Gender is required";
} else {
$Gender = test_input($_POST["Gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h3>FORM EXAMPLE IN PHP</h3>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
Name: <input type="text" name="Name">
<span class="error">* <?php echo $NameError;?></span>
<br><br>
E-mail: <input type="text" name="Email">
<span class="error">* <?php echo $EmailError;?></span>
<br><br>
Gender: <input type="radio" name="Gender" value="female">Female
<input type="radio" name="Gender" value="male">Male
<span class="error">* <?php echo $GenderError;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Output:

forms

Conclusion

In this article, we have learned different components of form and the methods to submit the form. The developer generally uses the GET method so that the user can see what content has been entered whereas in the POST method the case is different where the user details are not displayed on the screen.

Recommended Articles

This is a guide to PHP Form. Here we discuss the basic concept, how to create forms, methods, syntax of PHP form along with examples and code implementation. You may also look at the following articles to learn more-

Post a Comment

3 Comments

  1. Good details bro l love this article lo👌👌👌👌👌👌👌👍👍👍👍💕💕💕💕💕☀️☀️☀️☀️☀️☀️☀️💕💕💕💕💕👍👍👌👌👍👍👌👍👍👌👍👍👍👍👍👍👍👌👍👏👌👍👏👌👌👍👏👍👍👍👏👍👍👍👏👏👌👍👏👏👍

    ReplyDelete