0% found this document useful (0 votes)
13 views10 pages

Chapter 4.1-4.3

Uploaded by

34Shreya Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Chapter 4.1-4.3

Uploaded by

34Shreya Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 4

4.1 Creating a web page using GUI components,browser Role-GET and


POST methods,Server Role
The Web runs on HTTP, or Hyper Text Transfer Protocol. This protocol is a communication standard
between the client and the server. A web browser is used to send request for web page contents to web
server. A web server locates the requested contents and send response back to the client. A web server can
usually handle multiple simultaneous connections. When it is not communicating with a client, it spends its
time listening for an incoming connection. When any request arrives, the server sends back a response to
confirm its receipt.
Simple client - server request-response operation :

Each step in the request and response sequence is as follows:


1. User enter http://server.com into browser’s address bar.
2. Browser looks up the IP address for server.com.
3. Browser issues a request for the home page at server.com.
4. The request crosses the Internet and arrives at the server.com web server.
5. The web server, having received the request, looks for the web page on its hard disk.
6. The web page is retrieved by the server and returned to the browser.
7. Browser displays the web page.
For dynamic web pages : client - server request-response operation :

Steps for a dynamic client/server request/response sequence:


1. Enter http://server.com into browser’s address bar.
2. Browser looks up the IP address for server.com.
3. Browser issues a request to that address for the web server’s home page.
4. The request crosses the Internet and arrives at the server.com web server.
5. The web server, having received the request, fetches the home page from its hard disk.
6. With the home page now in memory, the web server notices that it is a file incorporating PHP scripting
and passes the page to the PHP interpreter.
7. The PHP interpreter executes the PHP code.
8. Some of the PHP contains MySQL statements, which the PHP interpreter now passes to the MySQL
database engine.
9. The MySQL database returns the results of the statements back to the PHP interpreter.
10. The PHP interpreter returns the results of the executed PHP code, along with the results from the
MySQL database, to the web server.
11. The web server returns the page to the requesting client, which displays it.

Variables :
Server configuration and request information—including form parameters and cookies—are accessible
from PHP scripts. Collectively, this information is referred to as EGPCS (environment, GET, POST, cookies,
and server).
PHP creates six global arrays that contain the EGPCS information. The global arrays are:
$_COOKIE : Contains any cookie values passed as part of the request, where the keys of the array are the
names of the cookies
$_GET : Contains any parameters that are part of a GET request, where the keys of the array are the names
of the form parameters
$_POST : Contains any parameters that are part of a POST request, where the keys of the array are the
names of the form parameters
$_FILES : Contains information about any uploaded files
$_SERVER : Contains useful information about the web server.
$_ENV : Contains the values of any environment variables, where the keys of the array are the names of the
environment variables such as database connectivity variables
These variables are not only global, but are also visible from within function definitions.
The $_REQUEST array is also created by PHP automatically. The $_REQUEST array contains the elements
of the $_GET, $_POST, and $_COOKIE arrays all in one array variable.
Server Information : The $_SERVER array contains a lot of useful information from the web server. Much
of this information comes from the environment variables required in the CGI specification.

Entries in $_SERVER that come from CGI:


PHP_SELF : The name of the current script. This variable is useful when creating self-referencing scripts.
SERVER_SOFTWARE : A string that identifies the server (“Apache/1.3.33 (Unix) mod_perl/1.26 PHP/ 5.0.4”).
SERVER_NAME : The hostname, DNS alias, or IP address for self-referencing URLs (www.example.com).
GATEWAY_INTERFACE : The version of the CGI standard being followed (e.g., “CGI/1.1”).

Processing Forms : With PHP, form parameters are available in the $_GET and $_POST arrays.
Methods : There are two HTTP methods that a client can use to pass form data to the server: GET and
POST. The method that a particular form uses is specified with the method attribute to the form tag.
A GET request encodes the form parameters in the URL and is referred as a query string. In the URL, the
text that follows the ? is the query string: /path/to/abc.php?word=despicable&length=3. Because all form’s
parameters are encoded in the URL with a GET request, users can bookmark GET queries. The HTTP
specification says that GET requests are idempotent—that is, one GET request for a particular URL,
including form parameters, is the same as two or more requests for that URL. Thus, web browsers can cache
the response pages for GET requests, because the response page doesn’t change regardless of how many
times the page is loaded. Because of idempotence, GET requests should be used only for queries such as
splitting a word into smaller chunks or multiplying numbers, where the response page is never going to
change. The GET method is designed for retrieving information, such as a document, an image, or the
results of a database query, from the server.
A POST request passes the form parameters in the body of the HTTP request, does not add data to URL.
The most visible difference between GET and POST is the URL line. POST requests are not idempotent.
This means that they cannot be cached, and the server is re-contacted every time the page is displayed. This
makes POST requests the appropriate choice for queries whose response pages may change over time—for
example, displaying the contents of a shopping cart or the current messages in a bulletin board. The POST
method is meant for posting information, such as a credit card number or information that is to be stored in a
database, to the server.

Type of method used to request a PHP page is available through $_SERVER['REQUEST_METHOD'].


For example:
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
// handle a GET request
}

The following table lists the most important elements that can go inside $_SERVER:

Element/Code Description
$_SERVER['PHP_SELF'] Returns the filename of the currently executing script

$_SERVER['GATEWAY_INTER Returns the version of the Common Gateway Interface (CGI)


FACE']
the server is using

$_SERVER['SERVER_ADDR'] Returns the IP address of the host server

$_SERVER['SERVER_NAME'] Returns the name of the host server (such as www.w3schools.com)

$_SERVER['SERVER_SOFTWA Returns the server identification string (such as Apache/2.2.24)


RE']

$_SERVER['SERVER_PROTOC Returns the name and revision of the information protocol


OL']
(such as HTTP/1.1)

$_SERVER['REQUEST_METH Returns the request method used to access the page (such as POST)
OD']

$_SERVER['REQUEST_TIME'] Returns the timestamp of the start of the request

$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via a query string

$_SERVER['HTTP_ACCEPT'] Returns the Accept header from the current request

$_SERVER['HTTP_ACCEPT_C Returns the Accept_Charset header from the current request


HARSET']
(such as utf-8,ISO-8859-1)

$_SERVER['HTTP_HOST'] Returns the Host header from the current request

$_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not reliable because
not all user-agents support it)

$_SERVER['HTTPS'] Is the script queried through a secure HTTP protocol

$_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the current
page

$_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the current
page

$_SERVER['REMOTE_PORT'] Returns the port being used on the user's machine to communicate
with the web server

$_SERVER['SCRIPT_FILENAM Returns the absolute path name of the currently executing script
E']
$_SERVER['SERVER_ADMIN'] Returns the value given to the SERVER_ADMIN directive in the web
server configuration file (if your script runs on a virtual host, it will
be the value defined for that virtual host)

$_SERVER['SERVER_PORT'] Returns the port on the server machine being used by the web server
for communication (such as 80)

$_SERVER['SERVER_SIGNAT Returns the server version and virtual host name which are added
URE']
to server-generated pages

$_SERVER['PATH_TRANSLAT Returns the file system based path to the current script
ED']

$_SERVER['SCRIPT_NAME'] Returns the path of the current script

$_SERVER['SCRIPT_URI'] Returns the URI of the current page

Parameters (About data entered in form): Use the $_POST, $_GET, and $_FILES arrays to access form
parameters from your PHP code. The keys are the parameter names, and the values are the values of those
parameters.

Example : Form processing with html and php two separate files.

HTML file: abc.html PHP file : e1.php


<html> <html>
<body> <body>
<form action="e1.php" method="POST"> <?php
Enter Student Name : <input type="text" name="name"> $sname = $_POST['name'];
<br> $r = $_POST['rollno'];
Enter Roll Number : <input type="text" name="rollno"> echo "Student Name= ".$sname;
<br> echo "<br><br>";
<input type="submit" value="Submit"> echo "Student Roll Number= ".$r;
</form> ?>
</body> </body>
</html> </html>

Output : Output :
Self-Processing Pages : One PHP page can be used to both generate a form and process it.
<html>
<head><title>Square of a number</title></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
Enter a number: <input type="text" name="number" />
<br />
<input type="submit" value="Calculate Square" />
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$no= $_POST['number'];
$s = $no*$no;
echo "Square of $no = $s";
}
?>
</body>
</html>
Output:

4.3 Working with multiple forms :


1. A web page having many forms :
With multiple <form> tags many forms can be placed on a web page. Each <form> tag represents one form.
Each must have different name value for name attribute of <form> tag to differentiate it from other forms.
Example :
<form name=”f1”> … </form>
<form name=”f2”> … </form>

Example :
<html>
<body>
<form name="student" method="post" action="st.php">
Enter Student Name : <input type="text" name="sname">
<br>Enter Roll Number : <input type="text" name="rno">
<br><input type="submit" name="f1sub">
</form>
<hr><hr>
<form name="staff" method="post" action="stf.php">
Enter Staff Name : <input type="text" name="stname">
<br>Enter employee Number : <input type="text" name="eno">
<br><input type="submit" name="f2sub">
</form>
</body>
</html>
Output:

Processing data from different forms can be done by checking name and value of submit button. When
control passes to script, the data submitted through post method is transferred to the script and script can
access that data. By checking value of submit button programmer can find out which form is submitted and
then process the data.
Example :
Code for HTML document
<html>
<body>
<form name="student" method="post" action="process.php">
Enter Student Name : <input type="text" name="sname">
<br>Enter Roll Number : <input type="text" name="rno">
<br><input type="submit" name="submit" value="Submit student data">
</form>
<hr><hr>
<form name="staff" method="post" action="process.php">
Enter Staff Name : <input type="text" name="stname">
<br>Enter employee Number : <input type="text" name="eno">
<br><input type="submit" name="submit" value="Submit staff data">
</form>
</body>
</html>

Code for process.php script


<html>
<body>
<?php
if($_POST['submit']=="Submit student data")
{
echo "Welcome to student submit";
$sn=$_POST['sname'];
$r=$_POST['rno'];
echo "<br>Student Name = $sn";
echo "<br> Student Roll no = $r";
}
else if($_POST['submit']=="Submit staff data")
{
echo "Welcome to staff submit";
$sn=$_POST['stname'];
$e=$_POST['eno'];
echo "<br>Staff Name = $sn";
echo "<br> Staff Employee Number = $e";
}
else
{
echo "Invalid";
}
?>
</script>
</body>
</html>

Output of script :
Welcome to student submit
Student Name = swara
Student Roll no = 1

Output of script :

Welcome to staff submit


Staff Name = rutuja
Staff Employee Number = 30

2. A form having multiple submit buttons :


On one single web with single form can have multiple submit buttons to perform various tasks. All submit
button can have same name but value attribute must hold different values for each. By comparing value of
each button programmer can find out which task to be performed when data is submitted to script.

Method 1 :By checking value of the button. We can give same name to all submit buttons and compare its
value in script before processing data.
Example :
Html code :
<html>
<body>
<form name="student" method="post" action="process1.php">
Enter Student Name : <input type="text" name="sname">
<br>Enter Roll Number : <input type="text" name="rno">
<br><input type="submit" name="submit" value="Display name">
<input type="submit" name="submit" value="Display roll no">
</form>
<hr><hr>
</body>
</html>

Process1.php code :
<html>
<body>
<?php
if($_POST['submit']=="Display name")
{
echo "Entered Name : ";
$sn=$_POST['sname'];
echo "<br>Student Name = $sn";
}
else if($_POST['submit']=="Display roll no")
{
echo "Entered roll no : ";
$r=$_POST['rno'];
echo "<br> Student Roll Number = $r";
}
else
{
echo "Invalid";
}
?>
</script>
</body>
</html>

Output :
Clicked on Display name : Output :
Entered Name :
Student Name = swara

Clicked on Display roll no : Output :


Entered roll no :
Student Roll Number = 1

Method 2 : By checking name of the button. We can give different names to button and compare it in script.
Example :
Code for form :-
<html>
<body>
<form name="student" method="post" action="process2.php">
Enter Student Name : <input type="text" name="sname">
<br>Enter Roll Number : <input type="text" name="rno">
<br><input type="submit" name="submit1" value="Display name">
<input type="submit" name="submit2" value="Display roll no">
</form>
<hr><hr>
</body>
</html>

Code for script :-


<html>
<body>
<?php
if(isset($_POST['submit1']))
{
echo "Entered Name : ";
$sn=$_POST['sname'];
echo "<br>Student Name = $sn";
}
else if(isset($_POST['submit2']))
{
echo "Entered roll no : ";
$r=$_POST['rno'];
echo "<br> Student Roll Number = $r";
}
else
{
echo "Invalid";
}

?>
</script>
</body>
</html>

You might also like