0% found this document useful (0 votes)
34 views38 pages

PHP Chap2

Chapter Two covers HTML forms and server-side scripting with PHP, detailing conditional operators, loops, and arrays. It explains how to create and process HTML forms, emphasizing the differences between GET and POST methods for data submission. Additionally, it outlines the server's role in handling requests and processing form data using PHP.

Uploaded by

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

PHP Chap2

Chapter Two covers HTML forms and server-side scripting with PHP, detailing conditional operators, loops, and arrays. It explains how to create and process HTML forms, emphasizing the differences between GET and POST methods for data submission. Additionally, it outlines the server's role in handling requests and processing form data using PHP.

Uploaded by

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

Chapter Two

HTML Forms and Server Side Scripting

Melak E.
Conditionals and Operators
 There is one more operator called the
conditional operator.
 It first evaluates an expression for a true or
false value and then executes one of the
two given statements depending upon the
result of the evaluation.
Cont’d…
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
$a = 10;
$b = 20;
/* If condition is true then assign a to result otherwise b */
$result = ($a > $b ) ? $a :$b;
echo "TEST1 : Value of result is $result<br/>";
/* If condition is true then assign a to result otherwise b */
$result = ($a < $b ) ? $a :$b;
echo "TEST2 : Value of result is $result<br/>";
?>
</body>
</html>
Cont’d…

 This will produce the following result:

TEST1 : Value of result is 20


TEST2 : Value of result is 10
PHP Operators
 Operators are used to operate on values. There
are four classifications of operators:
Arithmetic

 Assignment
Comparison

Logical
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Conditional statements
 Very often when you write code, you want to
perform different actions for different decisions.
use conditional statements.
 if statement - use this statement to execute some
code only if a specified condition is true
 if...else statement - use this statement to execute
some code if a condition is true and another code if
the condition is false
 if...else if....else statement - use this statement to
select one of several blocks of code to be executed
 switch statement - use this statement to select one
of many blocks of code to be executed
If()
 The following example will output "Have a
nice weekend!" if the current day is Friday:
<?php
$day=‘fri’;
if($day=“fri”)
{
echo “have a nice weekend”;
}
?>
If…else
 Use the if....else statement to execute some
code if a condition is true and another code if a
condition is false.
<?php
$day=‘fri’;
if($day=“fri”)
{
echo “have a nice weekend”;
}
else
{
echo “have a nice day”;
}
?>
If…..elseif….else
<?php
$day=‘fri’;
if($day=“fri”)
{
echo “have a nice weekend”;
}
else if($day=‘sun’)
{
echo “have a enjoy day”;
}
else
{
echo “have a nice day”;
}
?>
Switch….case
 The Switch statement in PHP is used to perform one of several
different actions based on one of several different conditions.
<?php
$num=5;
switch($num)
{
case 1:
echo "The number is one;
break;
case 2:
echo "The number is two ;
break;
default:
echo "No number found";
break;
}
?>
PHP Loop
 Loops in PHP are used to execute the same
block of code in a specified number of times.
PHP supports following four loop types.
 while - loops through a block of code while a
specified condition is true
 do...while - loops through a block of code once,
and then repeats the loop as long as a
specified condition is true
 for - loops through a block of code a specified
number of times
 foreach - loops through a block of code for
each element in an array
While loop
The while loop will execute a block of code if
and as long as a condition is true.
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br/>";
$i++;
}
?>
Do…while
 The do...while loop will execute a block of code at
least once-it then will repeat the loop as long as a
condition is true.
<?php
$var=0;
do
{
$var++;
echo "The number is " . $var . "<br/>";
}
while ($var<5);
?>
For loop
 The for loop is used when you know how many times
you want to execute a statement or a list of statements

Example :
<?php
for ($i=1; $i<=5; $i++)
{
echo $i;
}
?>
The foreach loop statement
 The foreach statement is used to loop through
arrays.
 For each pass the value of the current array
element is assigned to $value and the array
pointer is moved by one and in the next pass
next element will be processed.
Cont’d…
<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value )
{
echo "Value is $value <br />";
}
?>
PHP Array
 An array is a special variable, which can store
multiple values in one single variable.
 Instead of having many similar variables, you
can store the data as elements an array.
 Each element in the array has its own index so
that it can be easily accessed
 In PHP, there are three kind of arrays:
 Numeric array - An array with a numeric index
 Associative array - An array where each ID key is
associated with a value
 Multidimensional array - An array containing one
or more arrays
Numeric array
 A numeric array stores each array element
with a numeric index. by default, the array index
starts from zero.
<?php
$names = array(“PHP",“JAVA",“.NET");
or
$names[0] = “PHP";
$names[1] = “JAVA";
$names[2] = “.NET";
echo $names[0] . “, " . $names[1] . " and " .
$names[2] ." are languages";
?>
Associative Array
The associative arrays are very similar to numeric
arrays in term of functionality but they are
different in terms of their index.
When storing data about specific named values, a
numerical array is not always the best way to do
it.
With associative arrays we can use the values as
keys and assign values to them.
 To store the salaries of employees in an array, a
numerically indexed array would not be the best
choice.
 Instead, we could use the employees names as the
keys in our associative array, and the value would be
their respective salary.
Cont’d…

<?php
/* First method to associate create array. */
$salaries = array(
"mohammad" => 2000,
"kedir" => 1000,
"zara" => 500
);
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of kedir is ". $salaries['kedir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
/* Second method to create array. */
$salaries['mohammad'] = "high";
$salaries['kedir'] = "medium";
$salaries['zara'] = "low";
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of kedir is ". $salaries['kedir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
?>
Multidimensional array

 In a multidimensional array, each element in the main array can also be

an array. And each element in the sub-array can be an array, and so on.

<?php
$marks = array(
"mohammad" => array
(
"physics" => 35,
"maths" => 30,
"chemistry" => 39
),
"kedir" => array
(
"physics" => 30,
"maths" => 32,
"chemistry" => 29
),
Cont’d…

"zara" => array(


"physics" => 31,
"maths" => 22,
"chemistry" => 39
)
);
/* Accessing multi-dimensional array values */
echo "Marks for mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for kedir in maths : ";
echo $marks['kedir']['maths'] . "<br />";
echo "Marks for zara in chemistry : " ;
echo $marks['zara']['chemistry'] . "<br />";
?>
Cont’d…
Review of HTML Forms
 Forms are essential to any Web page that
communicates with users.
 When you order a book or a pillow, register for
a course, pay bills online, get directions to a
street address, you fill out a form.
 Once you have filled out the form, you push an
Order now! or Submit type button, and the
form is submitted.
 The information you entered is collected by the
browser, URL encoded, and sent to a server.
The server might then send the encoded
information to a program for processing
Cont’d…
 Working with forms is a two-step
process:
1. Create the HTML form.
2. Process the information entered into the
form.
 The Browser’s Role:
 HTML forms are created in a text file and
displayed in a browser window. They consist of
fields or buttons that request user interaction .
 After the user fills out the form, the form data is
URL encoded by the browser and submitted to a
server for further processing.
Creating HTML Forms

 To create an HTML form, you start in an editor with a filename


ending in .html or .htm
 The Steps to Produce a Form
 The following steps are essential in producing a form. The
next example illustrates how each of these steps is applied.
 Start: Start the form with the HTML <form>
tag.
 Action: The action attribute of the <form> tag
is the URL of the PHP script that will process
the data input from the form.
 Method: Provide an HTTP method on how to
process the data input.
 The default is the GET method, but the
POST method is most commonly used with
forms.
Cont’d…
 Create: Create the form with buttons,
boxes, and whatever looks attractive using
HTML tags and
fields.
 Submit: Create a submit button so that
the form can be processed. This will
launch the program
listed in the action attribute.
 End: End the form with the </form> tag.

End the HTML document with the


</html> tag.
Cont’d…
 There are different ways of sending this data to
the server called HTTP request methods. (HTTP
is the standard way of sending documents
over the Web.
 The GET and POST methods are the two most
common request types.
 Essentially these methods perform the same
function but how they handle the input data is
different.
The GET Method
 In GET method the data is sent as URL parameters that are

usually strings of name and value pairs separated by

ampersands (&).

 The GET method is restricted to send upto 1024 characters only.

 Never use GET method if you have password or other sensitive

information to be sent to the server.

 GET can't be used to send binary data, like images or word

documents, to the server.

 The PHP provides $_GET associative array to access all the sent

information using GET method.


Cont’d…
 The GET method sends form input in the URL.
The POST method
 In POST method the data is sent to the server as a
package in a separate communication with the
processing script.
 When the POST method is used, the browser does
not put the encoded data in a query string, but
rather bundles up the data and puts it into an HTTP
header message body.
 The POST is invoked by adding a METHOD attribute
to the <FORM> tag in your HTML document.
Cont’d…
 If using the POST method, the METHOD attribute
must be added to the HTML <form> tag
METHOD="POST"
 The POST method sends form input in an HTTP
header.
Cont’d…

The Server’s Role


 When the browser requests a Web page from the
server, it establishes a TCP/IP connection and sends
a request in an HTTP format. The server examines
the request and responds. The first line of the HTTP
request might look like this: GET /file.php HTTP/1.1
 This line specifies that the method to handle the
incoming data is GET, that the file to retrieve is
file.php, and that the version of HTTP is 1.1.
 PHP runs as part of the server and has access to the
form data passed from the browser to the server.
After the server gets the file, it sends it to PHP to be
processed
<?php
Echo”End of chapter 2 “;
?>

You might also like