003 Web Forms
003 Web Forms
Web
Engineering
WEB FORMS
Outline
oLogical Operator
o Operator precedence
o Array functions
o String
o Web forms
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
array_push($area,Genting,Melaka);
array_pop($area);
String
o Some functions to apply formatting:
o Two functions:
oPrintf()
o Sprintf()
String
o Type specifier
10
String
o Example :
printf("This is for test %d, 10); // decimal value 10
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:
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.
13
14
15
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{
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