Web Lab Notes - Upto Form Handling
Web Lab Notes - Upto Form Handling
PHP
Web Server
• A web server is a computer that runs websites.
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Comments in PHP
• In PHP,
– Use // to make a single-line comment.
– /* and */ to make a large comment block
Variables in PHP
Eg:
$color = "red";
PHP Operators
<html>
<body>
<?php
$x = 15;
$x% = 4;
echo $x;
?>
</body>
</html>
(c)Comparison operators
(d) Increment / Decrement Operators
e) Logical Operators
• The PHP logical operators are used to combine conditional
statements.
Example
(f) String Operators
Example
<?php
$txt1 = "Hello";
$txt2 = " world!";
echo $txt1 . $txt2;
?>
(g) Array Operators
PHP Conditional Statements
Example
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
The if...else if....else Statement
• The for loop is used when you know in advance how many times the
script should run.
Syntax
for (init counter; test condition; increment)
{
code to be executed;
}
Example
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
PHP Arrays
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
</body>
</html>
Associative Arrays
Method 1
• In this example we use an array to assign ages to
the different persons:
$ages = array("Peter"=>32, “Ben"=>30,
"Joe"=>34);
Method 2
$ages['Peter'] = "32";
$ages[‘Ben'] = "30";
$ages['Joe'] = "34";
The foreach Loop
• The foreach loop is used to loop through arrays.
• Works only on arrays, and is used to loop through
each key/value pair in an array.
Syntax
foreach ($array as $value)
{
code to be executed;
}
• For every loop iteration, the value of the current
array element is assigned to $value (and the array
pointer is moved by one) - so on the next loop
iteration, you'll be looking at the next array value.
Display array values
<?php
$size=array("Big","Medium","Short");
foreach( $size as $s )
{
echo "Size is: $s<br />";
}
?>
print_r()
<?php
$a = array("red", "green", "blue");
print_r($a);
echo "<br>";
Output:
Array ( [0] => red [1] => green [2] => blue )
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
count() or sizeof() function : which returns length of an array
<?php
$size=array("Big","Medium","Short");
echo count($size);
?>
Program to reverse an array
<?php
$array = array(1, 2, 3, 4);
$size = sizeof($array);
Name(“Arun", "1975");
Name(“Varun", "1978");
?>
Example
<?php
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it
will return 10
?>
<?php
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it
will return 10
?>
When a user clicks on the link "Test $GET", the parameters "subject" and
"web" are sent to "test_get.php", and we can then access their values in
"test_get.php" with $_GET.
test_get.php
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>
PHP $_REQUEST
• It is used to collect data after submitting an HTML form.
Example
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else { echo $name; }
}
?>
</body>
</html>
PHP Form Handling
• $_GET and $_POST are used to collect form-data or retrieve
information from forms, like user input.
PHP Form Handling
Example - HTML form with two input fields and a submit
button:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
When a user fills out the form and click on the submit button,
the form data is sent to a PHP file, called "welcome.php":
"welcome.php"
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
Output
Welcome John!
You are 28 years old.
PHP Form Validation
• PHP superglobals $_GET and $_POST are used to collect
form-data.
• An HTML form contains various input fields: (required and
optional) text fields, radio buttons, and a submit button:
The validation rules for the form above are: