PHP Form Handling

In this tutorial, we will learn about PHP Form Handling. In the last tutorial, we learned about GET and POST methods. Here this topic is totally related to our last topic. In this tutorial, we will create small example programs in which we will send data from HTML Form to webserver.

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 GET method. (In this tutorial we are not using any database but don’t worry in MYSQL tutorials we will also perform search operation in the database using PHP) )

In our next example program, we will create a login form using the POST method. Please keep it in your mind we always use the POST method for login, signup pages because we don’t want to show user data ain URL bar. (Here we will use a static username and password value but in MySQL tutorial we will perform the login by searching 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 signup page using POST method.

Spread the love