Computer Programming web Web programming Tips



PART I:How to use '$ _GET' and '$ _POST' methods

By Sergey Skudaev


In this tutorial you will learn how to use request variables $_GET, $_POST, $_COOKIE, $_REQUEST and how to use session in PHP

You can use different ways to transfer data from one web page to another. Let us start with Form methods: GET and POST. First we will use GET method Create a simple HTML form.

 

$_GET[]

<html>
<head>
<title>Form Methods
</title>
</head>
<body>
<form method="get" action="formoutputpage.php">
<p><input type=text name=greeting size="15"></p>
<p><input type=text name=name size="15"></p>
<p><input type=submit name=submit value="Salutation"></p>
</form>
</body>
</html>

Save it as form_methods.php file in your Apache htdocs/post folder created by you.

Let us create a formoutputpage.php file for output data transferred from the form.

<?

echo $_GET['greeting'];

echo $_GET['name'];
echo "!";
?>

Save this file in the same directory as form_methods.php file.

This form looks looks that:

HTML form example

Let us enter a greeting and a name and click Salutation button.
You can see that data sent from a form with the GET method is displayed in browser's address bar:

http://localhost/post/formoutputpage.php?greeting=Hello&name=Emily&submit=Salutation

Output web page displays Hello Emily!


$_POST[]

Let us change POST method instead of GET method. Edit form_method.php form.

<html>
<head>
<title>Form Methods
</title>
</head>
<body>
<form method="post" action="formoutputpage.php">
<p><input type=text name=greeting size="15"></p>
<p><input type=text name=name size="15"></p>
<p><input type=submit name=submit value="Salutation"></p>
</form>
</body>
</html>

Edit formoutputpage.php file as follow:

<?

echo $_POST['greeting']; echo " ".$_POST['name']; echo "!"; ?>

The browser address bar display formoutputpage.php, but no data transferred with POST Method is visible. Web page output will be the same:

Hello Emily!

PART II:$_COOKIE & $_SESSION

My eBooks on Amazon.com

US    UK    BR    CA
US    UK    BR    CA
US   UK   BR   CA