PHP is powered by forms. Forms enable a user to post information to another page or to the database. In this section, we’re just going to learn how information is sent to another page and in another post, we shall see how it is posted to the database. We already saw how forms are created in HTML here therefore, let us start creating one. Let us create two pages i.e., form.php and output.php.
form.php page
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form Handling</title>
</head>
<body>
<form action="output.php" method="post">
<input type="text" name="person">
<button>Hello</button>
</form>
</body>
</html>
The logic on this page is just HTML for creating a form. However, there are some two attributes inside the opening tag for form element i.e., action and method. The action attribute is used to specify the page on which the information will be submitted to which in our case is output.php yet the method is used to for posting the information posted in this form. There are two methods that can be used i.e., the get and post but the post is preferred due to its secure nature when submitting the information.
Inside the input field, there is a name attribute. This is extremely crucial when dealing with forms in PHP. The value in the name attribute is used when retrieving submitted information, for our case the name is person.
After coming up with the form, let us move to the the output.php file to write the logic that will enable the retrieval of information submitted in the form on form.php file.
output.php page
<?php
$name = $_POST['person'];
echo "Hello $name";
?>
In the output page, we start with a variable $name that stores the information submitted in the form. This information submitted in the form must start with $_POST (if post method was used) followed square brackets. Inside the square brackets is where we write the value of the name attribute of the field whose information submitted is needed, for our case it is person.
Now we can output the name of the person submitted in the form using the echo statement. So when running the program, we start from the page that has the form and we input details. On clicking submit, it will take us to the output.php page with the information posted.
Example 2: Let us get an advanced example for a simple program that bases on the points and gender of a student to determine if the student qualifies for state sponsorship. Let us create two file i.e. form.php and sponsorship.php
form.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form Handling</title>
</head>
<body>
<h1>Government Scholarship Checker</h1>
<form action="sponsorship.php" method="post">
<input type="text" name="firstName" placeholder="First Name">
<input type="text" name="gender" placeholder="Gender">
<input type="number" name="points" placeholder="Points">
<input type="text" name="disability" placeholder="Disability">
<button type="submit">Check</button>
</form>
</body>
</html>
sponsorship.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sponsorship Logic</title>
</head>
<body>
<h1>Government Sponsorship Checker</h1>
<?php
$points = $_POST['points'];
$gender = $_POST['gender'];
$disability = $_POST['disability'];
if ($points >= 18) {
echo "You qualify for government sponsorship. 🎉";
} elseif ($points >= 15 and $gender == "Female") {
echo "You qualify for government sponsorship. 🎉";
} elseif ($points >= 10 and $disability == "Yes") {
echo "You qualify for government sponsorship. 🎉";
} else {
echo "Sorry, you do not qualify.";
}
?>
<div>
<a href="forms.php">Back</a>
</div>
</body>
</html>
When you study the code above in sponsorship.php, you can see we have some HTML codes. There is no problem. PHP works with HTML and that is in fact what is done in most of the cases as long as PHP codes are wrapped inside <?php and ?>. You can add in some styling to your work if you wish so by adding some CSS and you link it to your files.
That is how we do form handling in PHP. Hope you enjoyed the experience 🤠. Go on and do work out something of your own!