web technology introduction to php. php php stands for php hypertext preprocessor php is a...

20
Web Technology Introduction to PHP

Upload: alan-franklin

Post on 04-Jan-2016

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Web Technology

Introduction to PHP

Page 2: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

PHP

• PHP stands for PHP Hypertext Preprocessor

• PHP is a Server-Side Web Scripting language, which executes on the web server.

• PHP can be embedded in HTML or used as a standalone binary

• PHP’s syntax is C-like and PHP’s variables are Perl-like

Page 3: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Insert PHP Codes in HTML<HTML>

<HEAD> <TITLE>My first HTML document</TITLE> </HEAD>

<BODY> <center><?php

echo “Hello World!!”; ?></center>

</BODY> </HTML>

Page 4: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Variable in PHP

<?$temperature = 28.9;echo “<center>”;echo “Today temperature is “.$temperature.” degrees.”;echo “</center>”;

$score = 88;echo “<P>My score is $score.</P>”;

?>

• PHP variables are not strictly typed

• All variables are started with “$” (dollar sign).

• There is no need to declare variables before using.

• String concatenation is done by “.” (dot).

Page 5: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Variable in PHP (cont)

$arr = array(1, 3, 5, 20);

echo “$arr[2]”;

• Array variables are loosely defined.• We can assign a value to the array directly.

$arr[0] = “Apple”;$arr[1] = “Mango”;$arr[4] = “Grape”;

• We can also initialize array variable using array() construct.

Page 6: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Associative Array• Array that stores element values in association with key values rather than a strict linear index order.

• The element ‘Bang Sue’ is stored in the array $location, in association with lookup key ‘KMUTNB’.

• Another example:

• The result of variable $my_arr has three values (1, 2, 3), each is stored in association with keys (0, ‘orange’, and 3)

$location[‘KMUTNB’] = ‘Bang Sue’;

$my_arr[0] = 1;$my_arr[‘orange’] = 2;$my_arr[3] = 3;

Page 7: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Branching

<?$score = 55;

if($score>=50) echo “You pass.”;else echo “You fail.”;

?>

• if-else

if (test)statement-1;

elsestatement-2;

if (test) {statement…;………….;

}else {

statement….;…………..;

}

Page 8: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Branching

switch($shape){ case 0:

echo “Circle.”;break;

case 1:echo “Triangle;break;

case 2:echo “Rectangle;break;

default:echo “Polygon”;

}

• Switch-Case

switch(expression){ case value-1:

statement;…….[break;]

case value-2:statement;…….[break;]

[default:]default-statement;…….

}

Page 9: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Looping (Iteration)

for($i=0; $i<10; $i++)echo “$i: Hello World!<BR>”;

• for loop

for (initial-expression; termination-check; loop-end-expression)statement;

for (initial-expression; termination-check; loop-end-expression) {statement;…………..

}

Page 10: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Looping (Iteration)

$i=0;while($i<10){

echo “$i: Hello World!<BR>”;$i++

}

• while loop

while(condition)statement;

while(condition) {statement;…………..

}

Page 11: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Looping (Iteration)

$i=0;do {

echo “$i: Hello World!<BR>”;$i++

} while($i<10);

• do-while loopdo statementwhile (expression);

do {statement;……….;

}while (expression);

Page 12: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Web Technology

HTML and PHP

Page 13: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Create Form in HTML

<form name=‘myform’ action=‘filename.php’ method=POST>…Form’s Objects…..………………………..

</form>

• Action:• Specify the target php file to process this form

• method:• GET: send data by appending them to the URL• POST: send data with the body of form

• Data not visible to user

Page 14: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Set Variable Name to Form’s Objects

• Text

• Select

• Radio Button (use same names for all in the group)

• Check Box (use array[] for all in the group)

<input type=‘text’ name=‘txtName’ value=‘’>

<select name=‘sltDept’ >

<input type=‘radio’ name=‘rdGender’ value=‘M’>Male<input type=‘radio’ name=‘rdGender’ value=‘F’>Female

<input type=‘checkbox’ name=‘chkHobby[]’ value=‘sport’>Sports<input type=‘checkbox’ name=‘chkHobby[]’ value=‘book’>Books<input type=‘checkbox’ name=‘chkHobby[]’ value=‘music’>Music

Page 15: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Example of a Form

<form action="test.php" method="post"><input type="text" name="txtName"><input type="radio" name="rdDept" value="TE">TE<input type="radio" name="rdDept" value="TM">TM<input type="radio" name="rdDept" value="TCT">TCT<input type="radio" name="rdDept" value="TTC">TTC<select name="sltDegree">

<option value="">=== Highest Degree ===</option><option value="B">Bachalor</option><option value="M">Master</option><option value="D">Doctoral</option>

</select><input type="checkbox" name="chkHobby[]" value="music">Music<input type="checkbox" name="chkHobby[]" value="sport">Sports<input type="checkbox" name="chkHobby[]" value="book">Books

</form>

Page 16: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Example of the Form

Page 17: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Retrieving Values from the Form in PHP

• If the method is “GET”

• If the method is “POST”

• We can use $_REQUEST[‘varname’] to get value, regardless of the method used in the form

• Let’s try this “phpinfo();”;

<?echo $_GET[‘varname’];

?>

<?echo $_POST[‘varname’];

?>

Page 18: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Retrieving Values from the Form in PHP

• Retrieving Values from a Text, Radio Button, and select is easy.

• Just refer to the object’s name (variable name) we want to retrieve the value from

<?echo $_POST[‘txtName’].”<BR>”;echo $_POST[‘rdDept’].”<BR>”;echo $_POST[‘sltDegree’].”<BR>”;

?>

Page 19: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

Retrieving Values from the Form in PHP

• Retrieving values from checkbox is a bit tricky

• We never know how many items would be checked– It is also possible that no items be selected at all

<?phpfor($i=0; $i < sizeof($_POST['chkHobby']); $i++)

echo $_POST['chkHobby'][$i]."<HR>";?>

$hobby = $_POST[‘chkHobby’];for($i=0; $i < sizeof($hobby); $i++)

echo $hobby[$i]."<HR>";

or

Page 20: Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web

So! What else?

• Make sure we validate data before submitting– Use javascript to check the validity of data before sending them

to the target PHP– This is to make data ready for processing in PHP

• How can we make the form look nicer?

• Use <table> or <style> to help display better output

• What happens if method is “GET”, instead?

• Try to play with $_REQUEST[‘xxx’], see what happens?

• What happens if we want to store these values in the database?