Lab 12
Lab 12
Create an HTML document which contain a text field for entering the “Id” of an employee. When the
user enters the “Id” of employee in the text field then make an AJAX request to a PHP script which
retrieves the details of concerned employee from the database and display the details in the web
page in tabular format.
<?php
$servername="127.0.0.1";
$username="root";
$password='';
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS University";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully\n";
} else {
echo "Error creating database: " . mysqli_error($conn) . "\n";
}
// Select database
mysqli_select_db($conn, "University");
// Create table
$sql = "CREATE TABLE IF NOT EXISTS Employee (
Id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Firstname VARCHAR(30) NOT NULL,
Lastname VARCHAR(30) NOT NULL,
Department VARCHAR(30) NOT NULL,
Designation VARCHAR(30) NOT NULL
)";
if (mysqli_query($conn, $sql)) {
echo "Table created successfully\n";
} else {
echo "Error creating table: " . mysqli_error($conn) . "\n";
}
// Insert data
$sql = "INSERT INTO Employee (Firstname, Lastname, Department,
Designation)
VALUES
('John', 'Doe', 'IT', 'Manager'),
('Jane', 'Doe', 'HR', 'Coordinator'),
('Bob', 'Smith', 'Finance', 'Analyst'),
('Alice', 'Johnson', 'Marketing', 'Manager'),
('David', 'Lee', 'IT', 'Developer'),
('Amy', 'Wang', 'Marketing', 'Coordinator'),
('Tom', 'Wilson', 'Finance', 'Manager'),
('Sarah', 'Liu', 'IT', 'Analyst'),
('Steve', 'Garcia', 'HR', 'Manager'),
('Cathy', 'Chen', 'IT', 'Coordinator')";
if (isset($_POST['id'])) {
$id = $_POST['id'];
if (mysqli_num_rows($result) > 0) {
echo "<table>";
echo "<tr><th>ID</th><th>First Name</th><th>Last
Name</th><th>Department</th><th>Designation</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row['Id'] . "</td><td>" . $row['Firstname']
. "</td><td>" . $row['Lastname'] . "</td><td>" .
$row['Department'] . "</td><td>" . $row['Designation'] .
"</td></tr>";
}
echo "</table>";
} else {
echo "No employee found with ID = $id";
}
}
mysqli_close($conn);
?>
Emp1.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Employee Details</title>
<script>
function getEmployeeDetails() {
var id = document.getElementById("employee-id").value;
</body>
</html>
Output:
2) Write a PHP script to access the data from the following “books.xml” file and display the data in
the following format using Expat parser
“books.xml”
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en-us">XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>
Q2as12.php
<?php
$xml = new XMLReader();
$xml->open('books.xml');
while ($xml->read()) {
if ($xml->nodeType == XMLReader::ELEMENT && $xml->name == 'book')
{
$category = $xml->getAttribute('category');
$title = '';
$author = '';
$year = '';
$price = '';
while ($xml->read()) {
if ($xml->nodeType == XMLReader::ELEMENT && $xml->name ==
'title') {
$title = $xml->readString();
}
if ($xml->nodeType == XMLReader::ELEMENT && $xml->name ==
'author') {
$author = $xml->readString();
}
if ($xml->nodeType == XMLReader::ELEMENT && $xml->name ==
'year') {
$year = $xml->readString();
}
if ($xml->nodeType == XMLReader::ELEMENT && $xml->name ==
'price') {
$price = $xml->readString();
}
if ($xml->nodeType == XMLReader::END_ELEMENT && $xml->name ==
'book') {
echo "Category: $category <br>";
echo "Title: $title <br>";
echo "Author: $author <br>";
echo "Year: $year <br>";
echo "Price: $price <br><br>";
break;
}
}
}
}
$xml->close();
?>
Output:
Books.xml: