Talk To Server - GET METHOD
GET and POST methods are used to communicate with web server. Today we will look at GET method.
day_7_ex_1.php
PHP Code:
<?php
// PHP use Array $_GET to accept input from browser.
// Lets print_r() value of $_GET array.
echo '<p>Value of $_GET array is: </p>';
echo '<pre>';
print_r($_GET);
echo '</pre>';
By default value of $_GET array will be empty.
Now access the page with link
Code:
http://YOUR-WEB-SITE/day_7_ex_1.php
You will see something like
This is because No value is passed to server using GET method.
Lets tell server something by adding a ?name=BIZHAT at the end of the url as follows.
Code:
http://YOUR-WEB-SITE/day_7_ex_1.php?name=BIZHAT.COM
You will see
Congratulations, you have successfully communicated with web server !
Replace BIZHAT.COM with your name or another text and experiment.
You can pass more than one value to server this way, separate NAME=VALUE paid with ampersand (&). For example
Code:
http://YOUR-WEB-SITE/day_7_ex_1.php?todo1=Learn&todo2=PHP&todo3=HTML&todo4=Java
As you can see, $_GET is an Associative Array.
User Friendly Way To Talk To Web Server
Most web site visitors don't want to learn how to use GET, so we have to make a nice HTML form page for them to use. So lets make a very simple HTML form page.
day_7_ex_2.html
HTML Code:
<html>
<body>
<h1>Welcome to my personal web page.</h1>
<p>Please enter you name below and click that button to see my personal page.</p>
<form method="GET" action="day_7_ex_2.php">
Enter Your Name: <input name="full_name" type="text">
<button type="submit">Enter Web Site</button>
</form>
</body>
</html>
In this HTML page, we set form method to GET (method="GET"). That means, when you submit form, data will be sent to web server using GET method.
action="day_7_ex_2.php" - This specify which PHP page will handle data submitted from the HTML form.
Lets create day_7_ex_2.php
day_7_ex_2.php
PHP Code:
<?php
echo '<h1>Hello, ' . $_GET['full_name'] . '! Welcome to my web site.</h1>';
It is important that you name this file same as specified in HTML form page action.
Why 2 Files ? Lets Combine
In previous example, we have one HTML page and a PHP page. Can't we combine both into one single PHP page ? Yes, we can.
We use isset() function in PHP to see if we have variable full_name passed to us, if not we will just show the HTML page to the visitor, so he can enter his name.
isset() function check if a variable is present or not.
day_7_ex_3.php
PHP Code:
<?php
if (isset($_GET['full_name'])) {
echo '<h1>Hello, ' . $_GET['full_name'] . '! Welcome to my web site.</h1>';
} else {
echo '
<html>
<body>
<h1>Welcome to my personal web page.</h1>
<p>Please enter you name below and click that button to see my personal page.</p>
<form method="GET" action="day_7_ex_3.php">
Enter Your Name: <input name="full_name" type="text">
<button type="submit">Enter Web Site</button>
</form>
</body>
</html>';
}
This code use isset() and if() functions in PHP. In HTML form code, we changed action to action="day_7_ex_3.php"
Bookmarks