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





Our dog needs urgent surgery, and the cost is overwhelming.

Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!



Oscar Story.

Oscar wasn’t just any puppy—he was a gift from a mother who trusted us with her smallest one.

For five years, my wife worked at the Indian Medical Center in Arizona, deep in Navajo Nation. Near her clinic, she often saw a homeless dog wandering the area. Over time, she began feeding her, and the dog grew fond of her. Then, one day, that same dog brought her newborn puppies to my wife—as if proudly showing them off.

Among them was the smallest, most delicate pup. My wife couldn’t resist. She brought him home, and we named him Oscar.

Oscar thrived in the house provided by the medical center, enjoying the big backyard where he lived. I built him a sturdy wooden doghouse, and we often took him on walks along the Window Rock Trail. He became our adventure companion, making the vast desert feel like home.

After my wife’s contract ended, we moved back to Florida, bringing Oscar with us. He adjusted to his new surroundings, but he never lost his adventurous spirit.

Now, Oscar faces a tough challenge—he needs urgent surgery, and the cost is overwhelming. We want to give him the best care possible, just as he’s given us years of joy and loyalty.

Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!



My  eBooks  on  Amazon

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