Avdhoot 22202C0059-1
Avdhoot 22202C0059-1
Experiment no.: 1
Title: a. Install and configure PHP, web server, MYSQL
b. Write a program to print “Welcome to PHP”
c. Write a simple PHP program using expressions and operators.
Ans:
You need a server that can interpret PHP. Common options include:
- XAMPP: A free and open-source cross-platform web server solution stack package
containing Apache, MySQL, and PHP.
- MAMP: A free, local server environment that can be installed under macOS and
Windows.
- WAMP: A Windows-only option that includes Apache, MySQL, and PHP.
ii. Set Up the Server:
Move your PHP files into the server's document root directory:
For XAMPP, it’s usually C:\xampp\htdocs\.
For MAMP, it’s typically Applications/MAMP/htdocs/.
For WAMP, it’s usually C:\wamp\www\.
iv. Access the PHP File through the Browser:
- The browser will send a request to the server, which will process the PHP code and
return the output (HTML, JSON, etc.) to the browser for display
2. What are the different operators supported by PHP with example? (any TWO
types)
Ans: PHP supports various types of operators, and two of the most commonly used ones are
Arithmetic Operators and Comparison Operators. Below are explanations with examples for
each:
Example:
<?php
$a = 10;
$b = 5;
Comparison Operators: Comparison operators are used to compare two values and return a
boolean value (true or false).
<?php
$a = 10;
$b = 5;
var_dump($a == $b); // Output: bool(false)
var_dump($a != $b); // Output: bool(true)
var_dump($a > $b); // Output: bool(true)
var_dump($a < $b); // Output: bool(false)
var_dump($a >= $b); // Output: bool(true)
var_dump($a <= $b); // Output: bool(false)
?>
Exercise:
Ans:
EXAMPLE:
<?php
$a = 10;
$b = 5;
$c = 10;
$d = "10";
$e = null;
$f = 15;
echo "Equal (==): ";
if ($a == $c) {
echo "$a is equal to $c\n"; // Output: 10 is equal to 10
} else {
echo "$a is not equal to $c\n";
}
echo "Identical (===): ";
if ($a === $d) {
echo "$a is identical to $d\n";
} else {
echo "$a is not identical to $d\n"; // Output: 10 is not identical to 10
}
(!=)echo "Not Equal (!=): ";
if ($a != $b) {
echo "$a is not equal to $b\n"; // Output: 10 is not equal to 5
} else {
echo "$a is equal to $b\n";
}
echo "Not Identical (!==): ";
if ($a !== $d) {
echo "$a is not identical to $d\n"; // Output: 10 is not identical to 10
} else {
echo "$a is identical to $d\n";
}
echo "Greater Than (>): ";
if ($a > $b) {
echo "$a is greater than $b\n"; // Output: 10 is greater than 5
} else {
echo "$a is not greater than $b\n";
}
ANS:
AND (&&): Both $a and $c are true, so the condition evaluates to true.
OR (||): $a is true, so the condition evaluates to true even though $b is false.
XOR (xor): Exactly one of $a or $b is true (not both), so it outputs "Exactly one of
$a or $b is true".
NOT (!): $d is false, so !$d evaluates to true.
Combined: In the final condition, the expression combines AND, OR, and NOT
operators. Since the left side of the OR is true ($a && $c), the entire condition
evaluates to true.
Example:
<?php
// Sample variables
$a = true; // This is true
$b = false; // This is false
$c = true; // This is true
$d = false; // This is false
// AND (&&) Operator
echo "AND (&&): ";
if ($a && $c) {
echo "\$a and \$c are both true.\n"; // Output: $a and $c are both true.
} else {
echo "One or both are false.\n";
}
// OR (||) Operator
echo "OR (||): ";
if ($a || $b) {
echo "At least one of \$a or \$b is true.\n"; // Output: At least one of $a or $b is true.
} else {
echo "Both \$a and \$b are false.\n";
}
// XOR (xor) Operator
echo "XOR (xor): ";
if ($a xor $b) {
echo "Exactly one of \$a or \$b is true.\n"; // Output: Exactly one of $a or $b is true.
} else {
echo "Either both are true or both are false.\n";
}
// NOT (!) Operator
echo "NOT (!): ";
if (!$d) {
echo "\$d is false, so NOT \$d is true.\n"; // Output: $d is false, so NOT $d is true.
} else {
echo "\$d is true.\n";
}
// Combined Logical Operators using AND, OR, and NOT
echo "Combined Logical Operators: ";
if (($a && $c) || (!$b && $d)) {
echo "The condition is true (Both \$a and \$c are true OR \$b is false and \$d is false).\n";
// Output: The condition is true (Both $a and $c are true OR $b is false and $d is false).
} else {
echo "The condition is false.\n";
}
// Another Complex Condition: Combining multiple logical operators
echo "Another Complex Condition: ";
if (($a && $b) || ($c && !$d) || $b) {
echo "Condition is true.\n";
} else {
echo "Condition is false.\n";
}
?>
Problem Statements:
B. Write a program to print “Welcome to PHP”
Code:
<?php
// Displaying strings
echo "Hello, Welcome to PHP Programming";
echo "<br/>";
//Displaying Strings as Multiple arguments
echo "Hello", " Welcome", " PHP";
echo "<br/>";
//Displaying variables
$s="Hello, PHP";
$x=10;
$y=20;
echo "$s <br/>";
echo $x."+".$y."=";
echo $x + $y;
?>
Output:
Code:
Arithmetic
<?php
$a = 20;
$b = 10;
echo "First number:$a <br/>";
echo "Second number:$b <br/>";
echo "<br/>";
$c = $a + $b;
echo "Addtion: $c <br/>";
$c = $a - $b;
echo "Substraction: $c <br/>";
$c = $a * $b;
echo "Multiplication: $c <br/>";
$c = $a / $b;
echo "Division: $c <br/>";
$c = $a % $b;
echo "Modulus: $c <br/>";
?>
Output:
Name: Avdhoot Nikam Lab no: L0001A
Roll no: 22202C0059 Subject Code: 22619
Course: IF6IC Subject Teacher: Chetashri Bhusari
Semester: 6 Subject: Web based application
development with PHP
Experiment no.: 2
Title: Write a PHP program to demonstrate the use of Decision making control
structures using
a. If statement
b. If-else statement
c. Switch statement.
Comparison Operator
Logical Operators
Ans: The code block inside the if statement is executed and The code inside the else block
will be skipped.
3. State the condition when the else part will be executed with example.
Ans: The else block will only run when the condition inside the if statement evaluates to
false. If the condition is true, the code inside the if block will run, and the else block will be
skipped.
Example:
<?php
$a = 3;
$b = 7;
if ($a > $b) {
echo "\$a is greater than \$b"; // This will NOT run because 3 is not greater than 7
} else {
echo "\$a is not greater than \$b"; // This will run because the condition $a > $b is false
}
?>
Exercise:
1. Write a program to make the use of logical operator
Ans:
<?php
// Sample variables
$a = true; // This is true
$b = false; // This is false
$c = true; // This is true
$d = false; // This is false
// OR (||) Operator
echo "OR (||): ";
if ($a || $b) {
echo "At least one of \$a or \$b is true.\n"; // Output: At least one of $a or $b is true.
} else {
echo "Both \$a and \$b are false.\n";
}
// XOR (xor) Operator
echo "XOR (xor): ";
if ($a xor $b) {
echo "Exactly one of \$a or \$b is true.\n"; // Output: Exactly one of $a or $b is true.
} else {
echo "Either both are true or both are false.\n";
}
Problem Statements:
1. Write a PHP program to demonstrate the use of Decision making control structures
using
Codes:
a. If statement
<?php
$a=10;
if ($a > 0)
{
echo "The number is positive";
}
?>
Output:
b. If-else
<?php
$a=-10;
if ($a > 0)
{
echo "The number is positive";
}
else
{
echo "The number is negative";
}
?>
Output:
c. Switch statement.
<?php
$ch=1;
$a=20;
$b=10;
switch ($ch)
{
case 1:
$c=$a + $b;
echo "Addition=".$c;
break;
case 2:
$c=$a - $b;
echo "Subtraction=".$c;
break;
case 3:
$c=$a * $b;
echo "Multiplication=". $c;
break;
case 4:
$c=$a / $b;
echo "Division=". $c;
break;
default:
echo "Wrong Choice";
}
?>
Output:
Name: Avdhoot Nikam Lab no: L0001A
Roll no: 22202C0059 Subject Code: 22619
Course: IF6IC Subject Teacher: Chetashri Bhusari
Semester: 6 Subject: Web based application
development with PHP
Experiment no.: 3
Title: Write a PHP program to demonstrate the use of Decision making control
structures using
a. While statement
b. Do-while statement
c. For statement.
d. Foreach Statement
Ans:
Problem Statements:
Write a PHP program to demonstrate the use of Decision making control structures
using
Codes:
a. While statement
<?php
//while loop to print even numbers
$i=0;
while ($i<= 10)
{
echo "$i<br/>";
$i+=2;
}
?>
Output:
b. Do-while statement
<?php
//do-while loop to print odd numbers
$i=1;
do
{
echo "$i<br/>";
$i+=2;
}while ($i< 10);
?>
Output:
c. for statement
<?php
//for loop to print even numbers and sum of them
$sum=0;
for($i=0; $i<=10;$i+=2)
{
echo "$i<br/>";
$sum+=$i;
}
echo "Sum=$sum";
?>
Output:
d. foreach statement
<?php
$arr = array (10, 20, 30, 40, 50);
foreach ($arr as $i)
{
echo "$i<br/>";
}
?>
Output:
Name: Avdhoot Nikam Lab no: L0001A
Roll no: 22202C0059 Subject Code: 22619
Course: IF6IC Subject Teacher: Chetashri Bhusari
Semester: 6 Subject: Web based application
development with PHP
Experiment no.: 4
Title: Write a PHP program for creating and manipulating
a) Indexed array
b) Associative array
c) Multidimensional array
Code:
<?php
$colors = ["Red", "Green", "Blue"];
echo $colors[0];
$person = ["name" => "student", "age" => 20];
echo $person["name"];
?>
Output:
Ans:
1.Indexed Arrays (Numeric Arrays):
• These arrays use numeric indexes (0, 1, 2, 3, etc.) by default.
• Eg:
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0]; // Output: Apple
2.Associative Arrays:
• These arrays use named keys (strings or any valid type) to store values, allowing you to
access elements using custom keys.
• Eg:
$person = ["name" => "John", "age" => 30, "city" => "New York"];
echo $person["name"]; // Output: John
3.Multidimensional Arrays:
• These are arrays that contain other arrays as their elements, allowing you to create more
complex structures like tables, matrices, or a combination of indexed and associative arrays.
• Eg:
$students = [
["name" => "Alice", "age" => 20],
["name" => "Bob", "age" => 22],
["name" => "Charlie", "age" => 23]
];
echo $students[0]["name"];
Exercise:
1. Develop a program to using Associative array.
Codes:
<?php
$ages = array(
"John" => 25,
"Alice" => 30,
"Bob" => 22,
"Eve" => 28
);
function addPerson(&$array, $name, $age) {
$array[$name] = $age;
echo "$name's age has been added.\n";
}
function getAge($array, $name) {
if (array_key_exists($name, $array)) {
return $array[$name];
} else {
return "Person not found.";
}
}
addPerson($ages, "Charlie", 35);
echo "John's age is: " . getAge($ages, "John") . "\n";
echo "Alice's age is: " . getAge($ages, "Alice") . "\n";
echo "Charlie's age is: " . getAge($ages, "Charlie") . "\n";
echo "David's age is: " . getAge($ages, "David") . "\n";
?>
Output:
Codes:
<?php
$matrix = array(
array(1, 2, 3), // First row
array(4, 5, 6), // Second row
array(7, 8, 9) // Third row
);
function printMatrix($matrix) {
foreach ($matrix as $row) {
foreach ($row as $value) {
echo $value . " ";
}
echo "<br>";
}
}
function sumOfMatrix($matrix) {
$totalSum = 0;
foreach ($matrix as $row) {
$totalSum += array_sum($row); // Sum of each row
}
return $totalSum;
}
function accessElements($matrix) {
echo "Accessing some specific elements:<br>";
echo "Element at (0, 0): " . $matrix[0][0] . "<br>"; // First element (1)
echo "Element at (2, 2): " . $matrix[2][2] . "<br>"; // Last element (9)
}
function modifyMatrix(&$matrix) {
echo "Modifying the matrix:<br>";
$matrix[1][1] = 55; // Change element at (1, 1) from 5 to 55
echo "Modified matrix:<br>";
printMatrix($matrix);
}
echo "Original Matrix:<br>";
printMatrix($matrix);
echo "<br>Sum of all elements in the matrix: " . sumOfMatrix($matrix) . "<br>";
accessElements($matrix);
modifyMatrix($matrix);
?>
Problem Statements:
Write a PHP program for creating and manipulating
Codes:
a) Indexed array
<html>
<head>
<title>
array
</title>
</head>
<body>
<h1> Creating an array </h1>
<?php
$course[0]="Computer Engg.";
$course[1]="Information Tech.";
$course[2]="Electronics and Telecomm.";
echo $course[2], "<BR>";
echo $course[0], "<BR>";
echo $course[1], "<BR>";
?>
</body>
</html>
Output:
b. Associative array
<html>
<head>
<title>
array
</title>
</head>
<body>
<h1> Creating an array </h1>
<?php
$course = array(1 => "Computer Engg.",
5 => "Information Tech.",
3 => "Electronics and Telecomm.");
echo $course [5];
?>
</body>
</html>
Output:
c. Multidimensional array
<?php
// Defining a multidimensional array
$person = array(
array(
"name" => "Yogita K",
"mob" => "5689741523",
"email" => "yogi_k@gmail.com",
),
array(
"name" => "Manisha P.",
"mob" => "2584369721",
"email" => "manisha_p@gmail.com",
),
array(
"name" => "Vijay Patil",
"mob" => "9875147536",
"email" => "Vijay_p@gmail.com",
)
);
// Accessing elements
echo "manisha P's email-id is: " . $person[1]["email"], "<br>";
echo "Vijay Patil's mobile no: " . $person[2]["mob"];
?>
Output:
Name:Avdhoot Nikam Lab no: L0001A
Roll no: 22202C0059 Subject Code: 22619
Course: IF6IC Subject Teacher: Chetashri Bhusari
Semester: 6 Subject: Web based application
development with PHP
Experiment no.: 5
Title: A. Write a PHP program to
• Calculate length of string.
• Count the number of words in string without using string functions.
B. Write a simple PHP program to demonstrate use of various built-in
string functions.
Ans:
1. strlen() : Returns the length of the string.
Syntax: strlen(string)
Example:
$str = "Hello";
echo strlen($str);
Code:
<?php
$number = -45;
echo "Absolute value of $number: " . abs($number) . "\n";
$number = 16;
echo "Square root of $number: " . sqrt($number) . "\n";
$base = 3;
$exponent = 4;
echo "$base raised to the power of $exponent: " . pow($base, $exponent) . "\n";
echo "Random number between 1 and 100: " . rand(1, 100) . "\n";
$floatNumber = 3.14159;
echo "Rounded value of $floatNumber: " . round($floatNumber) . "\n";
$values = array(10, 20, 30, 40, 50);
echo "Maximum value in the array: " . max($values) . "\n";
echo "Minimum value in the array: " . min($values) . "\n";
$angleInRadians = pi() / 4; // 45 degrees in radians
echo "Sine of $angleInRadians radians: " . sin($angleInRadians) . "\n";
$number = 100;
echo "Logarithm (base 10) of $number: " . log10($number) . "\n";
echo "Exponential of 2: " . exp(2) . "\n";
?>
Problem Statements:
A. Write a PHP program to
Output:
<?php
echo str_word_count("Welcome to PHP world!");
?>
Output:
B. Write a simple PHP program to demonstrate use of various built-in string functions.
<?php
$course[0]="Computer Engg.";
$course[1]="Information Tech.";
$course[2]="Electronics and Telecomm.";
$text=implode(",",$course);
echo $text;
?>
Output:
Experiment no.: 6
Title: Write a simple PHP program to demonstrate use of simple function and
parameterized function
Ans:
Ans:
$variable_name = function(parameters) {
// function body
return $value;
};
Exercise:
1. Write a program to demonstrate parameterized function.
<?php
function greet($name, $age) {
echo "Hello, $name! You are $age years old.\n";
}
greet("Alice", 25); // Passing "Alice" as name and 25 as age
greet("Bob", 30); // Passing "Bob" as name and 30 as age
?>
Problem Statements:
Write a simple PHP program to demonstrate use of simple function and parameterized
function
Codes:
Simple Function:
<?php
/* Defining a PHP Function */
function writeMessage(){
echo "Welcome to PHP world!";
}
/* Calling a PHP Function */
writeMessage();
?>
Output:
Parameterized Function:
<?php
function addfunc($num1, $num2)
{
$sum = $num1 + $num2;
echo "Sum of the two numbers is : $sum";
}
addfunc(50, 20);
?>
Output;
Name: Avdhoot Nikam Lab no: L0001A
Roll no: 22202C0059 Subject Code: 22619
Course: IF6IC Subject Teacher: Chetashri Bhusari
Semester: 6 Subject: Web based application
development with PHP
Experiment no.: 7
Title: Write a simple PHP program to create PDF document buy using
graphics concepts.
Problem Statements:
Write a simple PHP program to create PDF document buy using graphics concepts.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
Output:
Name: Avdhoot Nikam Lab no: L0001A
Roll no: 22202C0059 Subject Code: 22619
Course: IF6IC Subject Teacher: Chetashri Bhusari
Semester: 6 Subject: Web based application
development with PHP
Experiment no.: 8
Title: Write a PHP program to
a) Inherit members of super class in subclass.
b) Create constructor to initialize object of class by using object oriented
concepts.
3) Hierachical Inheritance
PHP supports Hierachical Inheritance. Hierarchical inheritance is the type of inheritance in
which a program consists of a single parent and more than one child class. Let’s understand
the same with this example. This type of inheritance in PHP language remains the same as
JAVA, C++, etc.
Exercise:
1. Write a program to demonstrate parameterized constructor.
Ans:
<?php
//PHP program to demonstrate the parameterized constructor.
class Sample
{
private $num1;
private $num2;
Problem Statements:
Write a PHP program to :
Code:
<?php
class a
{
function dis1()
{
echo "Hello PHP <br>";
}
}
class b extends a
{
function dis2()
{
echo "PHP Programming <br>";
}
}
$obj= new b();
$obj->dis1();
$obj->dis2();
?>
Output:
Code:
<?php
class MyClass
{
function __construct()
{
echo "Welcome to PHP constructor. <br />";
}
}
$obj = new MyClass; // This will display “Welcome to PHP constructor.”
?>
Output:
Name: Avdhoot Nikam Lab no: L0001A
Roll no: 22202C0059 Subject Code: 22619
Course: IF6IC Subject Teacher: Chetashri Bhusari
Semester: 6 Subject: Web based application
development with PHP
Experiment no.: 9
Title: Write a simple PHP program on Introspection and Serialization
Ans:- The serialization format in PHP is fairly simple. The serialized string consists of a
series of data types and values, each separated by a colon. For example, the serialized string
for an integer would be i:123, while the serialized string for a string would be s:5:”Hello”.
Exercise:
1) Develop a PHP code for serialization.
Ans:-
<?php
$var = array( 'Welcome', 568, array(1, 'two'), 'VP'); // a complex array
$string = serialize($var); // serialize the above data
print_r($string ."<br>"); // unserializing the data in $string
$newvar = unserialize($string); // printing the unserialized data
print_r($newvar);
?>
2)Develop a PHP code for introspection.
Ans:
<?php
class Rectangle
{
var $dim1 = 2;
var $dim2 = 10;
function Rectangle($dim1,$dim2)
{
$this->dim1 = $dim1;
$this->dim2 = $dim2;
}
function area()
{
return $this->dim1*$this->dim2;
}
function display()
{
// any code to display info
}
}
$S = new Rectangle(4,2);
//get the class varibale i.e properties
$class_properties = get_class_vars("Rectangle");
//get object properties
$object_properties = get_object_vars($S);
//get class methods
$class_methods = get_class_methods("Rectangle");
//get class corresponding to an object
$object_class = get_class($S);
print_r($class_properties);
print_r($object_properties);
print_r($class_methods);
print_r($object_class);
?>
Problem Statements:
Write a simple PHP program on Introspection and Serialization
Codes:
Introspection:
<?php
if (class_exists('Demo'))
{
$demo = new Demo();
echo "This is Demo class.";
}
else
{
echo "Class does not exist";
}
?>
Output:
Serialization:
<?php
$s_data= serialize(array('Welcome', 'to', 'PHP'));
print_r($s_data . "<br>");
$us_data=unserialize($s_data);
print_r($us_data);
?>
Output:
Name: Avdhoot Nikam Lab no: L0001A
Roll no: 22202C0059 Subject Code: 22619
Course: IF6IC Subject Teacher: Chetashri Bhusari
Semester: 6 Subject: Web based application
development with PHP
Experiment No: 10
Title of Design a web page using following form controls: a.Text box, b. Radio
Experiment button, c. Check box, d. Buttons
Exercise:
A.
Write a program to design a registration form using textbox, radio button, checkbox
and button.
B.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form</title>
</head>
<body>
<h2>Registration Form</h2>
</body>
</html>
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get form data
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$hobbies = isset($_POST['hobbies']) ? $_POST['hobbies'] : [];
// Process the data (for example, you can store it in a database or simply display it)
echo "<h2>Registration Successful!</h2>";
echo "<strong>Full Name:</strong> " . htmlspecialchars($fullname) . "<br>";
echo "<strong>Email:</strong> " . htmlspecialchars($email) . "<br>";
echo "<strong>Gender:</strong> " . htmlspecialchars($gender) . "<br>";
echo "<strong>Hobbies:</strong><br>";
if (count($hobbies) > 0) {
foreach ($hobbies as $hobby) {
echo "- " . htmlspecialchars($hobby) . "<br>";
}
} else {
echo "No hobbies selected.<br>";
}
} else {
// Redirect to the registration form if accessed directly
header("Location: exp10.html");
exit();
}
?>
Output:
Practical Related Questions:
A.
B.
a.
Textbox
b.
c.
Radio button
d.
e.
Check box
f.
g.
Buttons
h.
Ans:
Here's a brief explanation of each form control, along with their syntax and usage:
a.
Textbox
b.
(<input type="text">)
Use: A textbox is used to collect a single-line text input from the user, like a name, email, or
search query.
Syntax:
<input type="text" id="textbox" name="textbox" value="default value" placeholder="Enter
text">
Attributes:
type="text": Specifies that the input is a text field.
id: The unique identifier for the input field (for CSS or JavaScript targeting).
name: The name of the input field, which will be used to send the data when the form is
submitted.
value: The default value (optional).
placeholder: Provides a hint to the user about what to enter in the textbox (optional).
Example:
<label for="username">Username:</label>
<input type="text" id="username" name="username" placeholder="Enter your username">
b. Radio Button (<input type="radio">)
Use: A radio button allows the user to select one option from a predefined set of choices
(usually part of a group of radio buttons). Only one option can be selected at a time.
Syntax:
<input type="radio" id="option1" name="group" value="Option 1">
<label for="option1">Option 1</label>=
Attributes:
type="radio": Specifies that the input is a radio button.
name: Specifies a name for the radio button group (all radio buttons with the same name will
belong to the same group).
value: Specifies the value that will be sent when the form is submitted.
id: A unique identifier for the radio button (for labeling purposes).
checked: If present, it indicates that the radio button is selected by default.
Example:
<p>Choose your gender:</p>
<input type="radio" id="male" name="gender" value="Male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="Female">
<label for="female">Female</label>
Attributes:
type="checkbox": Specifies that the input is a checkbox.
name: Specifies the name of the checkbox, which is sent with the form data when submitted.
value: Specifies the value to send if the checkbox is checked.
id: A unique identifier for the checkbox (for labeling purposes).
checked: If present, it indicates that the checkbox is checked by default.
Example:
<label for="subscribe">Subscribe to newsletter:</label>
<input type="checkbox" id="subscribe" name="subscribe" value="Yes">
Use: A button is used to trigger a form submission or to perform other actions like resetting
the form or executing a JavaScript function when clicked.
Syntax:
<button type="submit">Submit</button>
or
<input type="button" value="Click Me">
Attributes:
type: Specifies the type of the button. Common types are:
submit: Submits the form.
reset: Resets the form to its initial state.
button: A generic button that can trigger JavaScript events.
value: The text displayed on the button (for <input type="button">).
onclick: A JavaScript event handler that specifies a function to be executed when the button
is clicked (for <button> and <input>).
Example:
<form action="/submit">
<input type="text" name="username" placeholder="Enter username">
<button type="submit">Submit</button>
</form>
Experiment No: 11
Title of Design a web page using following form controls: a. List
Experiment box, b. Combo box, c. Hidden field box
Write a program to design a form using list box, combo box
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
<option value="grapes">Grapes</option>
<option value="orange">Orange</option>
</select>
<br><br>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
<option value="purple">Purple</option>
<option value="orange">Orange</option>
</select>
<br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fruit = $_POST['fruit'];
if (count($colors) > 0) {
}
} else {
} else {
header("Location: exp10.html");
exit();
?>
a. List Box
A List Box allows users to select one or multiple options from a predefined list of options. It
is implemented using the <select> element with the multiple attribute for multi-selection.
Use:
It is typically used when you want to allow users to select one or multiple options from a list
of choices, such as selecting multiple interests or favorite items.
Syntax:
<label for="listbox">Choose your favorite colors:</label>
<select id="listbox" name="colors[]" multiple size="5">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
<option value="purple">Purple</option>
</select>
b. Hidden Field
A Hidden Field is an HTML element that allows you to store data in a form that the user
cannot see or modify. It's useful for storing information that needs to be sent with the form
but is not directly visible or editable by the user.
Use:
Hidden fields are often used for maintaining state information (like session IDs, form
identifiers, etc.) when the form is submitted.
Syntax:
<input type="hidden" name="user_id" value="12345">
c. Combo Box
A Combo Box is a drop-down list that allows the user to select one option from a list. It is
implemented using the <select> element. By default, it only allows a single option to be
selected, but it can also be styled to let the user type in a custom entry using JavaScript.
Use:
A combo box is ideal when you want to offer a list of options, but also allow the user to
manually enter a value if needed (depending on how it's implemented).
Syntax:
<label for="fruit">Choose your favorite fruit:</label>
<select id="fruit" name="fruit">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
<option value="grapes">Grapes</option>
<option value="orange">Orange</option>
</select>
Summary:
List Box: A multi-select dropdown that lets users select one or multiple options.
Hidden Field: A non-visible input field used to store data in a form.
Combo Box: A dropdown list that allows users to select an option from the list, typically used
when there are many options.
Experiment No: 12
Title of Experiment Develop web page with data validation
Program code
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = trim($_POST["email"]);
if (empty($email)) {
} else {
?>
<!DOCTYPE html>
<html>
<head>
<title>Email Validation</title>
</head>
<body>
<form method="post">
</form>
</body>
</html>
Prq
Enlist and explain different method provided by php for web page validation.
1. Server-Side Validation
Server-side validation ensures that data is properly validated on the backend before
processing or storing it in a database. It is more secure than client-side validation.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
if (empty($name)) {
} else {
?>
<form method="post">
<input type="submit">
</form>
<input type="submit">
</form>
<script>
function validateForm() {
if (name == "") {
return false;
</script>
Used for filtering and validating data, such as email, URLs, integers, etc.
Example: Validating an Email Address
$email = "example@domain.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
} else {
(b) preg_match()
Experiment No: 16
Title of Develop a simple application to Update, Delete table data from
Experiment database.
<?php
if ($conn->connect_error) {
} else {
$conn->close();
?>
<?php
$conn = new mysqli("localhost", "username", "password", "database_name");
if ($conn->connect_error) {
} else {
$conn->close();
?>
Exercise
1.
2.
<?php
if ($conn->connect_error) {
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['id']) && isset($_POST['name']) &&
isset($_POST['email'])) {
$id = intval($_POST['id']);
$name = trim($_POST['name']);
$email = trim($_POST['email']);
if ($stmt->execute()) {
} else {
$stmt->close();
} else {
$id = 1;
$row = $result->fetch_assoc();
} else {
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Student</title>
</head>
<body>
<form method="post">
</form>
</body>
</html>
<?php
$conn->close();
?>