1) Write a simple PHP program which implements Ajax for addition of two numbers.
script.js
add.html
<html>
<head>
<script type="text/javascript">
function Addition()
{
var ob1=false;
ob1=new XMLHttpRequest();
var
no1=document.getElementById("no1").value;
var
no2=document.getElementById("no2").value;
ob1.open("GET","add.php?num1="+no1+"&num2="+no2);
ob1.send();
ob1.onreadystatechange=function()
{
if(ob1.readyState==4 && ob1.status==200)
document.getElementById("add").innerHTML=ob1.responseText; }
}
</script>
</head>
<body>
Enter first no :<input type=text name=no1
id="no1"><br>
Enter second no :<input type=text name=no2
id="no2"><br>
<input type=button name=submit value=add
onClick="Addition()"><br>
<span id="add"></span>
<body>
</html>
add.php
<?php
$n1=$_GET['num1'];
$n2=$_GET['num2'];
if((!empty($n1)) && (!empty($n2)))
{
$add=$n1+$n2;
echo "Addition = ".$add;
}
else "enter both nos";
?>
2) Create an application that reads
“Book.xml” file into simple XML object. Display attributes and elements(Hint:
use simple_xml_load_file() function)
book.css
book{
font-size: 4pt; font-weight:Bold;color:red;}
table
{ color:red; }
book.php
<html>
<head>
<link
rel="stylesheet" type="text/css"
href="book.css">
</head>
<body>
<?php
$xml=simplexml_load_file('Book.xml')or
die("cannot die");
?>
<center></b>Book details</b></center>
<table
border="1">
<th>Book
code</th>
<th>bname</th>
<th>Author</th>
<th>Publisher</th>
<?php
foreach($xml->book
as $a)
{
echo"<tr><td>".$a['code']."</td>";
echo"<td>".$a->bname."</td>";
echo"<td>".$a->authorname."</td>";
echo"<td>".$a->publisher."</td></tr>";
}
?>
</table>
</body>
</html>
Book.xml
<?xml
version="1.0" ?>
<?xml-stylesheet
type="text/css" href="book.css"?>
<bookinfo>
<book
code="B1">
<bname>MOE</bname>
<authorname>p.k.sinha</authorname>
<publisher>Techmax</publisher>
</book>
<book
code="B2">
<bname>Java</bname>
<authorname>Alok
pawar</authorname>
<publisher>Techmax</publisher>
</book>
</bookinfo>