0% found this document useful (0 votes)
47 views28 pages

003 Web Forms

The document discusses various topics related to web forms and PHP including: - Logical operators and precedence in PHP expressions - Array functions like count(), array_push(), array_pop() - String formatting functions like printf(), sprintf() - Form handling in PHP using the $_POST and $_FILES superglobal variables - Uploading files in PHP by specifying the enctype and accessing data in $_FILES - Embedding PHP code in HTML forms to process form data with scripts

Uploaded by

Fluez27
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)
47 views28 pages

003 Web Forms

The document discusses various topics related to web forms and PHP including: - Logical operators and precedence in PHP expressions - Array functions like count(), array_push(), array_pop() - String formatting functions like printf(), sprintf() - Form handling in PHP using the $_POST and $_FILES superglobal variables - Uploading files in PHP by specifying the enctype and accessing data in $_FILES - Embedding PHP code in HTML forms to process form data with scripts

Uploaded by

Fluez27
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/ 28

WEB202D

Web
Engineering
WEB FORMS

Outline
oLogical Operator
o Operator precedence
o Array functions
o String
o Web forms

Have you submitted your


proposal?

Logical Operators
o To create complex test expressions:

1 or 0 1
1 and 0 0
1 xor 0 1
4

Logical Operators
o Example:

$a=42;
$b=10;
If ($a<$b || $b==10) {
Echo Hello students;
}

Operator precedence
o Reading operator from left to right in PHP by default.
o Example:
4+5
4+5*2
(4 + 5 ) * 2

Some array functions


o count() and sizeof() : count number of elements in an array.
o Example:
$area=array("Kuala Lumpur", "Petaling Jaya", "Serdang");
count($area); // what is the resul?

oReset(): bring the pointer to the beginning of array.


oArray_push() : add one or more elements to the end of an existing array.
oArray_pop(): remove the last element of array.
o Example:

array_push($area,Genting,Melaka);
array_pop($area);

Some array functions


o array_merge(): combine two or more existing arrays.
o Example:
$area=array("Kuala Lumpur", "Petaling Jaya", "Serdang");
$places=array(KLCC,Pavilion);
$test=array_merg($area,$places); // What is the output?
oShuffle(): randomize the elements of array.
o Example:
$test2 = shuffle($test);

String
o Some functions to apply formatting:

o Two functions:
oPrintf()
o Sprintf()

oPrintf : accepts format control string and different type.


o Specification begins with a percent (%).

String
o Type specifier

10

String
o Example :
printf("This is for test %d, 10); // decimal value 10

printf("This is for test %f, 10); // double value 10.000000


printf("This is for test %b, 10); // binary value 01010
printf(%x%s,10,15); // binary value A , 15

11

String
o Example:

$teststring = 543;
printf ("Decimal: %d", $teststring); // 543
printf (Binary: %b", $teststring); // 1000011111
printf (Double: %f", $teststring); // 543.000000
printf (Octal: %o", $teststring); // 1037
o Field width : an integer value after percent sign to make spaces.
o Example:

printf("%20s \n", "Sunway"); // What is the result?


printf("%20s \n", "University");

12

String
o Precision : floating-point format.

o Example :
printf("%.2f ", 3.555555); // Output is 3.55
o sprintf : storing a formatted string in a variable to reuse it.

o Its like printf.


o Example:
o$test3 = sprint(%.2f,3.4000); // what is the output?

13

Some string functions


o strlen(): to determine the length of a string.
o Example:
$test4="Sunway;
if (strlen($test4)==5) {
echo "thank you"; }
else {

echo" University name must have 5 characters";


}

14

Some string functions


o strstr(): finding a string within another string.
o Example:
$test5="Sunway;
if (strstr($test4, way)) ,
echo Its existed"; }
else {

echo" Its not existed";


}

15

Some string functions


o strpos(): finding the position of string within another string.
o Example:
o test6=John Jackson;
o test7 = strpos($test6,son);
o substr(): extracting part of string.
o Example:
o $test8 = Sunway water park;
o echo substr($test8,9); // output is ter park

16

Forms
o HTML forms : a way to pass information from user to the server.
o A form can contain input elements such as text, password, textareas, checkboxes, radio buttons,
buttons, reset buttons, submit buttons, files, hidden files and images.
o We have separated HTML and PHP file but will be connected.
o HTML file will collect and send user information to PHP file for processing.

o Example: HTML
o <html>
o<head>
o<title> Simple1 HTML form </title>

o</head>
o<body>
o<form action="php2.php" method="POST">

17

Forms
o<p><strong>Name:</strong><br> // <p> for paragraph
o<input type="text" name="user"></p>
o<p><strong>Address:</strong><br>
o<textarea name="address" rows="5" cols="40"></textarea></p> //multiline text
o<p><input type="submit" value="send"></p>
o</form>
o</body>
o</html>

18

Forms
oExample: PHP

<?php
// $_GET and $_POST are super global variables.
echo "<p> Welcome <b> $_POST[user] </br></p>"; //collect data from form
echo "<p> your address is:<br> <b>$_POST[address]</b></p> //collect data from
form

?>

19

Forms
o Embedding PHP code within HTML.
o Example : a script to take a number and tell whether the submitted number is greater or smaller than predefined value.
o <?php

o$num_to_guess = 42;
o$message = "";
oif (!isset($_POST[guess])) { // If the guess is empty, the following message will be displayed.
o $message = "Welcome to the guessing machine!";
o} else if ($_POST[guess] > $num_to_guess{

o $message = "$_POST[guess] is too big! try a smaller number";


o} elseif ($_POST[guess] < $num_to_guess) {
o $message = "$_POST[guess] is too small! try a larger number";
o} else {
o $message = "Matched, Well done";
o}
o?>
20

Forms
o<html>
o<head>
o<title> PHP number guessing script </title>
o</head>
o<body>
o<h1> // for heading (header 1)
o<?php echo $message ?> // Print the message for the first time.
o</h1>
o<form action = "<?php echo $_SERVER[PHP_SELF] ?>" method = "POST"; // PHP_SELF parameter in $_SERVER reloads the current script file.
o<p> <strong> Type your guess number here:</strong>
o<input type = "text" name = "guess"> </p>
o<p> input type = "submit" value = "Submit your guess"> </p>
o</body>
o</html>

21

Forms
oOutput:

22

Forms
oPHP supports file uploads.
o$_FILES superglobal variable is used for uploading the file to the server.
o Example: firs file calls the second file to upload.
<html>
<head>
<title> A simple file upload form </title>
</head>
<body>
<form action = "upload.php" enctype ="multipart/form-data" method="POST"> // enctype is used to encode data in HTML for sending by POST.
<input type = "hidden" name = "MAX_FILE_SIZE" value = "51200"> // hidden field to define the maximum size of file (50 KB).
<p><strong> File to Upload:</strong>
<input type = "file" name ="fileupload"> </p>
<p> <input type = "submit" value = "upload!"> </p>
</form>
</body>
</html>
23

Forms
oOutput :

24

Forms
o Second file : upload.php
<html>
<head>
<title>uploadphp</title>
</head>
<body>
<h1>File Upload Results</h1>
<?php
$file_dir = /path/to/upload/directory; //creat the path to upload the file on the server.

foreach($_FILES as $file_name => $file_array) { // a loop to check every elements in $_FILE. Name of file$file_name
// file information _$file_array
echo path: .$file_array*tmp_name+.<br>\n;
echo name: .$file_array*name+.<br>\n;
echo type: .$file_array*type+.<br>\n;
echo size: .$file_array*size+.<br>\n;
25

Forms
echo file was moved!<br><br>;
}
}
?>
</body>
</html

26

Forms
oOutput :

27

Q&A

End.

28

You might also like