Google News
logo
PHP Form Handling
The PHP superglobals $_GET and $_POST are used to collect form-data.

The form element in HTML is automatically available to PHP scripts. The data in the form is stored in the superglobal array variables like $_GET or $_POST. These are the global array variables that can be accessed anywhere in the script, may it be a function, a class or a file without doing any special task. Hence called superglobals. As they are array variables, they can store multiple values in them.

PHP POST method

This is the built in PHP super global array variable that is used to get values submitted via HTTP POST method.
The array variable can be accessed from any script in the program; it has a global scope.
This method is ideal when you do not want to display the form post values in the URL.
Syntax
<?php
 $_POST['variable_name'];
?>
Example :
<!DOCTYPE html>
<html>
<head>
	<title>PHP POST Method</title>
</head>
<style type="text/css">
	.basic_form{ padding:5px 10px; border-radius:2px; margin:10px 0px 5px;}
	.cus_btn{ padding:6px 15px; background:#0099da; color:#FFF;}
	.cus_btn_1{ padding:6px 15px; background:#F30; color:#FFF;}
</style>
<body>

<form action="post_method_link.php" method="post">
    <input type="text" name="name" class="basic_form" placeholder="Name" /><br />
    <input type="email" name="email_id" class="basic_form" placeholder="example@gmail.com" /><br />
    <input type="number" name="mobile" class="basic_form" placeholder="996X666X68" /><br />
    <textarea rows="3" cols="40" name="message" class="basic_form" placeholder="Message"></textarea><br />
    <input type="submit" value="SUBMIT" class="cus_btn" />
    <input type="reset" value="CLEAR" class="cus_btn_1" />   
</form>

</body>
</html>
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "post_method_link.php". The form data is sent with the HTTP POST method.
post_method_link.php
<!DOCTYPE html>
<html>
<head>
	<title>PHP POST Method</title>
</head>
<body>

<h2>Welcome  To  <?php echo $_POST["name"]; ?></h2>
<h4>Email Id : <?php echo $_POST["email_id"]; ?></h4>
<h4>Mobile No : <?php echo $_POST["mobile"]; ?></h4>
<h4>Message : <?php echo $_POST["message"]; ?></h4>

</body>
</html>
Output :

PHP GET method

This is the built in PHP super global array variable that is used to get values submitted via HTTP GET method.

The array variable can be accessed from any script in the program; it has a global scope.

This method displays the form values in the URL.

Syntax
<?php
$_GET['variable_name'];
?>
Example :
<!DOCTYPE html>
<html>
<head>
	<title>PHP GET Method</title>
</head>
<style type="text/css">
	.basic_form{ padding:5px 10px; border-radius:2px; margin:10px 0px 5px;}
	.cus_btn{ padding:6px 15px; background:#0099da; color:#FFF;}
	.cus_btn_1{ padding:6px 15px; background:#F30; color:#FFF;}
</style>
<body>

<form action="get_method_link.php" method="get">
    <input type="text" name="name" class="basic_form" placeholder="Name" /><br />
    <input type="email" name="email_id" class="basic_form" placeholder="example@gmail.com" /><br />
    <input type="number" name="mobile" class="basic_form" placeholder="996X666X68" /><br />
    <textarea rows="3" cols="40" name="message" class="basic_form" placeholder="Message"></textarea><br />
    <input type="submit" value="SUBMIT" class="cus_btn" />
    <input type="reset" value="CLEAR" class="cus_btn_1" />   
</form>

</body>
</html>
Your inserted PHP file name is "get_method_link.php".
get_method_link.php
<!DOCTYPE html>
<html>
<head>
	<title>PHP GET Method</title>
</head>
<body>

<h2>Welcome  To  <?php echo $_GET["name"]; ?></h2>
<h4>Email Id : <?php echo $_GET["email_id"]; ?></h4>
<h4>Mobile No : <?php echo $_GET["mobile"]; ?></h4>
<h4>Message : <?php echo $_GET["message"]; ?></h4>

</body>
</html>
Output :