AIT Practical2 (Parth)
AIT Practical2 (Parth)
RollNo: 2301060
Q1. write a program to show current data and time using defined module in node js.
Code:
const fs = require('fs');
function getCurrentDateTime() {
const currentDate = new Date();
function main() {
const currentDateTime = getCurrentDateTime();
console.log('Current Date and Time:', currentDateTime);
}
main();
Q2. . write a php program to store the username in a cookie and check whether the user
has successfully logged in or not.
Code:
<?php
function setCookieUsername($username) {
setcookie("username", $username, time() + 3600, "/");
}
function isLoggedIn() {
return isset($_COOKIE['username']);
}
if (isset($_POST['submit'])) {
$username = $_POST['username'];
setCookieUsername($username);
header("Location: logged_in.php");
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Username: <input type="text" name="username"><br><br>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>
Q3. Write a program in NodeJS to perform file CRUD operation by using fs module.
Code:
const fs = require('fs');
const path = require('path');
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "your_database";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM employees WHERE department = 'Sales' AND salary BETWEEN 50000
AND 90000";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $row["id"];
$name = $row["name"];
$department = $row["department"];
$salary = $row["salary"];
$insert_sql = "INSERT INTO another_table (id, name, department, salary) VALUES ('$id',
'$name', '$department', '$salary')";
if ($conn->query($insert_sql) === TRUE) {
echo "Record inserted successfully<br>";
} else {
echo "Error inserting record: " . $conn->error . "<br>";
}
}
} else {
echo "No employees found in the Sales department with salary between 50000 and
90000<br>";
}
$conn->close();
?>
Q5. Write a PHP script to design Employee Registration form. Insert 5 records in
database and display all the inserted records on new page.
Code:
employee_registration_form.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Registration Form</title>
</head>
<body>
<h2>Employee Registration Form</h2>
<form action="insert_employee.php" method="POST">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br>
<label for="designation">Designation:</label><br>
<input type="text" id="designation" name="designation" required><br>
<label for="salary">Salary:</label><br>
<input type="number" id="salary" name="salary" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
insert_employee.php:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST['name'];
$designation = $_POST['designation'];
$salary = $_POST['salary'];
$sql = "INSERT INTO employees (name, designation, salary) VALUES ('$name', '$designation',
'$salary')";
$conn->close();
?>
display_employees.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Records</title>
</head>
<body>
<h2>Employee Records</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Designation</th>
<th>Salary</th>
</tr>
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["name"] . "</td><td>" . $row["designation"] . "</td><td>" .
$row["salary"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</table>
</body>
</html>
Q6. Create an Angular program which will demonstrate the use of ngswitch directive.
Code:
switch-demo.component.html
<div [ngSwitch]="color">
<p *ngSwitchCase="'red'">You selected Red</p>
<p *ngSwitchCase="'blue'">You selected Blue</p>
<p *ngSwitchCase="'green'">You selected Green</p>
<p *ngSwitchDefault>Invalid color selection</p>
</div>
<select [(ngModel)]="color">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
switch-demo.component.ts
@Component({
selector: 'app-switch-demo',
templateUrl: './switch-demo.component.html',
styleUrls: ['./switch-demo.component.css']
})
export class SwitchDemoComponent {
color: string = 'red'; // Default color selection
}