What is GET and POST method in php?

Types of method to get form data in php

GET and POST method.


GET and POST methods are used for getting the value of form.

both method name are passed in html form method

syntax :


Example :

<form action="index.php" name="frm" method="get"></form>
<form action="index.php" name="frm" method="post"></form>

PHP $_GET can also be used to collect form data after submitting an HTML form with method="get".

$_GET can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with parameters:

Example :

Output :-

<html>
<body>

<a href="mypage.php?site=itechxpert&web=ittutorial.in">Test $GET Super Global Variable</a>

<a href="mypage.php?id=<?php echo $id;?>">Click Here/a>
</body>
</html>

When a user clicks on the link "Test $GET Super Global variable", the parameters "site" and "web" are sent to "mypage.php", and you can then access their values in "mypage.php" with $_GET. The example below shows the code in "mypage.php":

<html>
<body>

<?php
echo "learn " . $_GET['site'] . " at " . $_GET['web'];
?>

</body>
</html>


PHP $_POST

PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.

Example :

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit" name="submit" value="Save" >
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// if( isset($_POST['submit']) )
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>

</body>
</html>

PHP $_REQUEST[' ']

HTTP Request variables. $_REQUEST[' '] is used to access the data from form after submited data by $_GET, $_POST and $_COOKIE.

Example :

<html>
<body>

<form action="test.php" method="post">
Your name: <input type="text" name="yourname" />
<input type="submit" />
</form>

</body>
</html>

<?php
<?php echo $_REQUEST ["yourname"]; ?>!
?>

Example :

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit" name="submit" value="Save" />
</form>

<?php
if (isset($_POST["submit"])) {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>

</body>
</html>

Example :

// Better use if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// POST method
}


if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// GET method
}

if $_POST

if( isset($_POST['submit_data']) )
{
$name = $_POST['name'];
echo $name;
}



if (!empty($_POST))
{

$name = $_POST['name'];
echo $name;
}





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here