PHP Form Handling

$_GET & $_POST are global variables which are used to obtain form data.

Simple HTML Form

Below form will consist of single text box which contains the name and send the data to welcome.php by get Method


<!doctype html>
<html>
	<head>
		<title>Simple Form</title>
	</head>
	<body>
		<form action="welcome.php" method="get">
			Name:<input type="text" name="txtName" />
			<br />
			<input type="submit" Value="Send" name="btnsend" />
			<input type="reset" />
		</form>
		</form>
	</body>
</html>

Welcome.php File for Form

$_GET store all the request's data which are sent by Get Method.


<?php
	if(isset($_GET['btnsend'])){
			echo 'Welcome Mr.'.$_GET['txtName'].' to PHPdocs.com';
		}
?>

Simple HTML Login Form

Below is the Login form which will send it's data to login.php file via post Method.


<!doctype html>
<html>
	<head>
		<title>Simple Form</title>
	</head>
	<body>
		<form action="login.php" method="POST">
			User Name:<input type="text" name="txtUserName" />
			<br />
			Password:<input type="password" name="txtPassword" />
			<br />
			<input type="submit" Value="Login" name="btnlogin" />
			<input type="reset" />
		</form>
	</body>
</html>

login.php file for Login Form


	<?php
		if(isset($_POST['btnlogin'])){
			$username=$_POST['txtUserName'];
			$password=$_POST['txtPassword'];
			
			if($username=="abc@gmail.com" && $password=="123@pk"){
				echo "Welcome to Page";
			}else{
				echo "Username/Password is Invalid. try again!";
			}
		}
	?>