3. Server-Side Scripting (PHP)
3. Server-Side Scripting (PHP)
INPUTS: sop1php.html
<!DOCTYPE html>
<html>
<head>
<title>SOP1 PHP </title>
</head>
<body>
<H1> Age Check to Vote ! ! ! </h1>
<form method="post" action="agechk.php">
Enter my age :
<input type="text" name="txt_age" >
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Output:
INPUTS: agechk.php
<?php
if (isset($_POST['submit']))
{
$age = $_POST['txt_age'];
if($age>=18)
{
echo "You are Eligible for Vote";
}
else
{
echo "You are not Eligible for Vote";
}
}
?>
Output:
SOP 2 : Write a PHP function to count the total number of vowels (a,e,i,o,u) from the string.
Accept a string by using HTML form.
INPUTS: sop2php.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vowel Counter</title>
</head>
<body>
<h1>Vowel Counter</h1>
<form action="vowel_count.php" method="post">
<label for="inputString">Enter a string:</label>
<textarea id="inputString" name="inputString" rows="4" cols="50"
required></textarea><br><br>
<input type="submit" value="Count Vowels">
</form>
</body>
</html>
Output:
INPUTS: vowel_count.php
<?php
function countVowels($str) {
// Convert string to lowercase to handle both uppercase and
lowercase vowels
$str = strtolower($str);
INPUTS: sop3.php
<?php
// Sample associative array
$associativeArray = array(
"first" => "Apple",
"second" => "Banana",
"third" => "Cherry",
"fourth" => "Date",
"fifth" => "Elderberry"
);
INPUTS: sop4.php
<?php
// Function to calculate the total marks and percentage
function calculateResults($marksArray) {
$totalMarks = array_sum($marksArray);
$totalSubjects = count($marksArray);
$percentage = ($totalMarks / ($totalSubjects * 100)) * 100; //
Assuming each subject is out of 100 marks
return array(
'totalMarks' => $totalMarks,
'percentage' => $percentage
);
}
// Calculate results
$results = calculateResults($marksArray);
INPUTS: sop5.php
<?php
// Define marks for each student
$studentsMarks = array(
'Student1' => array(
'English' => 85,
'Hindi' => 78,
'Marathi' => 88,
'Maths' => 92,
'Information Technology' => 90
),
'Student2' => array(
'English' => 75,
'Hindi' => 82,
'Marathi' => 80,
'Maths' => 85,
'Information Technology' => 88
),
'Student3' => array(
'English' => 90,
'Hindi' => 84,
'Marathi' => 91,
'Maths' => 87,
'Information Technology' => 93
),
'Student4' => array(
'English' => 78,
'Hindi' => 70,
'Marathi' => 75,
'Maths' => 80,
'Information Technology' => 85
),
'Student5' => array(
'English' => 88,
'Hindi' => 76,
'Marathi' => 82,
'Maths' => 90,
'Information Technology' => 89
)
);
Output:
SOP 6 : Write a program using PHP to calculate Electricity bill by accepting the limits.
• For first 100 units - Rs. 4
• For next 100 units - Rs. 5
• For next all units - Rs. 6
INPUTS: sop6php.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Electricity Bill Calculator</title>
</head>
<body>
<h1>Electricity Bill Calculator</h1>
<form action="calculate_bill.php" method="post">
<label for="units">Enter number of units consumed:</label>
<input type="number" id="units" name="units" min="0"
required>
<input type="submit" value="Calculate Bill">
</form>
</body>
</html>
Output:
INPUTS: calculate_bill.php
<?php
// Function to calculate electricity bill
function calculateElectricityBill($units) {
$bill = 0;
return $bill;
}
INPUTS: sop7php.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Student Data Insertion</title>
</head>
<body>
<h1>Insert Student Data</h1>
<form action="insert_student.php" method="post">
<label for="roll_number">Roll Number:</label>
<input type="text" id="roll_number" name="roll_number"
required><br><br>
<label for="name">Student Name:</label>
<input type="text" id="name" name="name"
required><br><br>
<input type="submit" value="Insert">
</form>
</body>
</html>
Output:
INPUTS: insert_student.php
<?php
// Database connection parameters
$host = 'localhost'; // or your database host
$port = '5432'; // default PostgreSQL port
$dbname = 'studentdb';
$user = 'your_db_user'; // replace with your PostgreSQL username
$password = 'your_db_password'; // replace with your PostgreSQL
password
if (!$conn) {
die("Error: Unable to connect to the database.");
}
if ($result) {
return "Student data inserted successfully.";
} else {
return "Error: " . pg_last_error($conn);
}
}
// Validate input
if (!empty($rollNumber) && !empty($name)) {
// Insert student data into the database
$message = insertStudent($rollNumber, $name, $conn);
} else {
$message = "Both roll number and name are required.";
}