Day 7 - Talk To Server - Become PHP Expert in 30 days
Talk To Server - GET METHOD
http://webhostingneeds.com/tmp_fp/php_day_7.jpg
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
http://webhostingneeds.com/tmp_fp/php_day_7_1.jpg
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
http://webhostingneeds.com/tmp_fp/php_day_7_2.jpg
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"
http://webhostingneeds.com/tmp_fp/php-back-btn.jpg
News subscription subscription using isset() and $_GET
http://php.flashwebhost.com/sibichan...bscription.php
PHP Code:
<?php
if (isset($_GET['email'])) {
echo 'Subscription request has been received' ;
}
else {
echo '
<h1>News Letter Subscription</h1>
<p>Please enter Email Address below and click that button to subscribe newsletter.</p>
<form action="subscription.php" method="GET">
Enter Email Address : <input name="email" type="text">
<button type="submit"> Subscribe </button>
</form>
';}
PHP GET with Bootstrap Form
An attempt with bootstrap forms and php GET method :tennis:
http://php.flashwebhost.com/melbin/d...dent_data.html
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>STUDENT DATA</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.css" />
<script type="text/javascript" href="js/bootstrap.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="span6 offset3">
<h2 style="text-align:center;">Student Data</h2>
</div>
</div>
<hr>
<div class="row">
<div class="span6 offset3">
<form class="form-horizontal" method="GET" action="print_progress_report.php">
<div class="control-group">
<label class="control-label" for="studentname">STUDENT NAME</label>
<div class="controls">
<input type="text" id="studentname" name="studentname" placeholder="Enter Student Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="std" name="std">STD</label>
<div class="controls">
<select name="std">
<option>SELECT STD</option>
<option>STD V</option>
<option>STD VI</option>
<option>STD VII</option>
<option>STD VIII</option>
<option>STD IX</option>
<option>STD X</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="std">BATCH</label>
<div class="controls">
<select name="division">
<option>SELECT BATCH</option>
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="language1">FIRST LANGUAGE</label>
<div class="controls">
<input type="text" name="language1" placeholder="Enter Mark between 0 and 100">
</div>
</div>
<div class="control-group">
<label class="control-label" for="language2">SECOND LANGUAGE</label>
<div class="controls">
<input type="text" name="language2" placeholder="Enter Mark between 0 and 100">
</div>
</div>
<div class="control-group">
<label class="control-label" for="physics">PHYSICS</label>
<div class="controls">
<input type="text" name="physics" placeholder="Enter Mark between 0 and 100">
</div>
</div>
<div class="control-group">
<label class="control-label" for="chemistry">CHEMISTRY</label>
<div class="controls">
<input type="text" name="chemistry" placeholder="Enter Mark between 0 and 100">
</div>
</div>
<div class="control-group">
<label class="control-label" for="biology">BIOLOGY</label>
<div class="controls">
<input type="text" name="biology" placeholder="Enter Mark between 0 and 100">
</div>
</div>
<div class="control-group">
<label class="control-label" for="maths">MATHEMATICS</label>
<div class="controls">
<input type="text" name="maths" placeholder="Enter Mark between 0 and 100">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Print Progress Report</button>
<button type="reset" class="btn">Reset All</button>
</div>
</form>
</div><!-- span6 -->
</div><!-- .row -->
</body>
</html>
http://php.flashwebhost.com/melbin/d...ess_report.php
PHP Code:
<?php
if (isset($_GET['studentname'])) {
$studentname=$_GET['studentname'];
} else {
echo 'NO NAME';
}
if (isset($_GET['std'])) {
$std=$_GET['std'];
} else {
echo 'No Data Found';
}
if (isset($_GET['division'])) {
$division=$_GET['division'];
} else {
echo 'No Data Found';
}
if (isset($_GET['language1'])) {
$language1=$_GET['language1'];
} else {
echo 'No Data Found';
}
if (isset($_GET['language2'])) {
$language2=$_GET['language2'];
} else {
echo 'No Data Found';
}
if (isset($_GET['physics'])) {
$physics=$_GET['physics'];
} else {
echo 'No Data Found';
}
if (isset($_GET['chemistry'])) {
$chemistry=$_GET['chemistry'];
} else {
echo 'No Data Found';
}
if (isset($_GET['biology'])) {
$biology=$_GET['biology'];
} else {
echo 'No Data Found';
}
if (isset($_GET['maths'])) {
$maths=$_GET['maths'];
} else {
echo 'No Data Found';
}
$gtotal=$language1+$language2+$physics+$chemistry+$biology+$maths;
echo '<hr>';
echo '<b>' . strtoupper($studentname) . ' ' . $std . ' ' . $division . '</b> <br>';
echo 'FIRST LANGUAGE : ' . $language1 . '<br>';
echo 'SECOND LANGUAGE : ' . $language2 . '<br>';
echo 'PHYSICS : ' . $physics . '<br>';
echo 'CHEMISTRY : ' . $chemistry . '<br>';
echo 'BIOLOGY : ' . $biology . '<br>';
echo 'MATHS : ' . $maths . '<br>';
echo '<hr>';
echo '<b> GRAND TOTAL : ' . $gtotal . '<b><br>';
echo '<hr>';
echo '<hr>';