Slip 27 - B) Write a PHP script to read book.XML and print book details in tabular format using simple XML(Content of book.XML are book_code, book_name,author, year price).

Solution:

Html File:

<html>
<script type="text/javascript">
function display()
{
name=f1.txt.value;
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp= new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
        xmlhttp.open("GET","slip27-p3-q2.php?txt="+ name,false);
        xmlhttp.send();
        document.getElementById('result').innerHTML=
        xmlhttp.responseText;
}
</script>
<body>
<form name="f1">
Enter Book name:
<input type="text" name="txt" onKeyUp="display()"/><br>
</form>
<div id='result'></div> 
</body>
</html>

PHP File:

<?php
$msearch=$_GET['txt'];
$xml=simplexml_load_file("slip27-p2-q2.xml");
echo $xml->getName()."<br>";
$hint="";
$len=strlen($msearch!=="");
if($msearch !=="")
{
foreach($xml->children() as $movie)
{
//if(strstr($movie->MovieName,substr($msearch,0,$len)))
if(strcmp($msearch,$movie->Title)==0)

{
$hint=" Book Name: $movie->Title <br>
        Book Author: $movie->Author <br>
        Year: $movie->Year<br>";
}
}
}
 echo $hint===""?"no suggestion":$hint;
 ?>


XML File:

<?xml version="1.0"?>
<BOOKS>
<BOOK>
<Title>OS</Title>
<Author>Nirali</Author>
<Year>2016</Year>
<Price>250</Price>
</BOOK>
<BOOK>
<Title>Advance PHP</Title>
<Author>M.K</Author>
<Year>2020</Year>
<Price>300</Price>
</BOOK>
<BOOK>
<Title>C++</Title>
<Author>Nirali</Author>
<Year>2020</Year>
<Price>350</Price>
</BOOK>
</BOOKS>


Post a Comment

0 Comments