PHP Form Handling

In PHP GET and POST superglobals are used to collect form-data.

In our first example program, we will create a search web page using the get method. let’s start with the search page example.

Example Program:

<?php
if(isset($_GET["submit"])){
    echo "<h2>Your are searching for " . $_GET["search"] . "</h2>";
}
?>
<html>
<body>
<form method="GET">
<input type="text" name="search" placeholder="Search Product Here"><br>
<input type="Submit" name="submit">
</form>
</body>
</html>

As you can see in the above example how we create a search page using the GET method. (In this tutorial we are not using any database but don’t worry in MYSQL tutorials we will also perform search operations in the database using PHP) 

In our next example program, we will create a login form using the POST method. Please keep in your mind we always use the POST method for login, and signup pages because we don’t want to show user data in the URL bar. (Here we will use a static username and password value but in the MySQL tutorial, we will perform the login by searching the user from the database)

Example Program: –

<?php
$valid_email="admin@xyz.com";
$valid_password="123";

if(isset($_POST["submit"])){
	$email=$_POST["email"];
	$password=$_POST["pass"];
	   if($valid_email==$email&&$valid_password==$password){
        echo "<h2>Welcome, your are a valid user</h2>";
	   }
	   else{
	    echo "<h2>Please check your email and password</h2>";
	   }
}
?>
<html>
<body>
<form method="POST">
<input type="text" name="email" placeholder="Please Enter Your Email"><br>
<input type="password" name="pass" placeholder="Please Enter Your Password"><br>
<input type="submit" name="submit">
</form>
</body>
</html>

As we created this login page in the same way you can create a signup page using the POST method.

Spread the love
Scroll to Top
×