0% found this document useful (0 votes)
4 views

php

Uploaded by

nirajkrsafi1999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

php

Uploaded by

nirajkrsafi1999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

LAB REPORT

Course Name: MCA Sem: 3rd


Web Technology Using PHP Lab
MCAN-E394D

Submitted By: Niraj Kr Safi


University/Class Roll no: 29171023019
Submitted To: Prof. Sanjib Bose

Department of Computer Applications


Netaji Subhash Engineering College
Session: 2023-24
MCA
rd
3 Semester
Web Technology Using PHP Lab
MCAN-E394D
Assignment-2

Sl No List of Programs Date Signature

1 Write javascript program to perform form validation for all


type of form inputs.

2 Write a PHP script to generate a list of first 20 prime number

3 Write a PHP script to count the number of vowels in a given


string.

4 Write a PHP script to create Employee table and insert 3


records into it using MYSQL.

5 Write a PHP script to select all the data stored in the


"Student" table where course = “PHP”

6 Write a PHP script to create a simple login page.


Assignment-2

1. Write javascript program to perform form validation for all type of form inputs.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation</title>
<script>
function validateForm() {
let name = document.getElementById("name").value;
let email = document.getElementById("email").value;
let password = document.getElementById("password").value;
let phone = document.getElementById("phone").value;
let gender = document.querySelector('input[name="gender"]:checked');
let country = document.getElementById("country").value;
let message = document.getElementById("message").value;

if (name == "") {
alert("Name is required");
return false;
}

// Validate Email
let emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
if (!emailPattern.test(email)) {
alert("Please enter a valid email");
return false;
}

// Validate Password
if (password.length < 6) {
alert("Password should be at least 6 characters");
return false;
}
// Validate Phone
let phonePattern = /^[0-9]{10}$/;
if (!phonePattern.test(phone)) {
alert("Please enter a valid phone number");
return false;
}
// Check if gender is selected
if (!gender) {
alert("Please select your gender");
return false;
}
// Validate country selection
if (country == "") {
alert("Please select your country");
return false;
}
// Validate message (optional)
if (message == "") {
alert("Message cannot be empty");
return false;
}
alert("Form submitted successfully!");
return true;
}
</script>
</head>
<body>

<h2>Form Validation Example</h2>


<form onsubmit="return validateForm()">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br><br>

<label for="phone">Phone Number:</label><br>


<input type="text" id="phone" name="phone"><br><br>
<label>Gender:</label><br>
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female<br><br>
<label for="country">Country:</label><br>
<select id="country" name="country">
<option value="">Select a country</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="India">India</option>
</select><br><br>
<label for="message">Message:</label><br>
<textarea id="message" name="message"></textarea><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
2. Write a PHP script to generate a list of first 20 prime number

<?php
function isPrime($num) {
if ($num <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
return false;
}
}
return true;
}

$primeNumbers = [];
$num = 2;
while (count($primeNumbers) < 20) {
if (isPrime($num)) {
$primeNumbers[] = $num;
}
$num++;
}

echo "First 20 Prime Numbers: <br>";


echo implode(", ", $primeNumbers);
?>

Output:
First 20 Prime Numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71
3. Write a PHP script to count the number of vowels in a given string.

<?php
function countVowels($str) {
$vowels = ['a', 'e', 'i', 'o', 'u'];
$count = 0;

$str = strtolower($str);

// Count the vowels in the string


for ($i = 0; $i < strlen($str); $i++) {
if (in_array($str[$i], $vowels)) {
$count++;
}
}
<?php
function countVowels($string) {
$vowels = ['a', 'e', 'i', 'o', 'u'];
$count = 0;
for ($i = 0; $i < strlen($string); $i++) {
if (in_array(strtolower($string[$i]), $vowels)) {
$count++;
}
}
return $count;
}

$string = "Hello, how are you?";


echo "Number of vowels in '$string': " . countVowels($string);
?>

Output:
Number of vowels in 'Hello, how are you?': 7
4. Write a PHP script to create Employee table and insert 3 records into it using
MYSQL.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Create Employee table


$sql = "CREATE TABLE Employee (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
position VARCHAR(50) NOT NULL,
salary INT(10) NOT NULL
)";

if ($conn->query($sql) === TRUE) {


echo "Table Employee created successfully<br>";
} else {
echo "Error creating table: " . $conn->error . "<br>";
}

// Insert records
$sql = "INSERT INTO Employee (name, position, salary) VALUES
('Alok Sharma', 'Manager', 5000),
('Satyam Sharma', 'Developer', 4000),
('Jyoti Choudhary', 'Designer', 3500)";

if ($conn->query($sql) === TRUE) {


echo "Records inserted successfully<br>";
} else {
echo "Error inserting records: " . $conn->error . "<br>";
}

$conn->close();
?>

Output:
5. Write a PHP script to select all the data stored in the “Student” table where course =
“PHP”

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "school_db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Select data where course is PHP


$sql = "SELECT * FROM Student WHERE course = 'PHP'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Course: " . $row["course"].
"<br>";
}
} else {
echo "No results found";
}

$conn->close();
?>
Output:
ID: 101 - Name: Satyam Sharma - Course: PHP
ID: 104 - Name: Shiwani Choudhary - Course: PHP

6. Write a PHP script to create a simple login page.


<?php
$valid_username = "admin";
$valid_password = "password123";

// Start session
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$password = $_POST['password'];

if ($username == $valid_username && $password == $valid_password) {


$_SESSION['loggedin'] = true;
echo "Login successful!";
} else {
echo "Invalid username or password.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form method="POST">
Username: <input type="text" name="username" required><br><br>
Password: <input type="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>

<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header("Location: index.php");
exit();
}

echo "Welcome, you are logged in!";


?>
Output:

You might also like