0% found this document useful (0 votes)
472 views8 pages

AJAX

The document describes three AJAX examples: 1) A PHP script that uses AJAX to validate a username and password from a database on login. 2) An AJAX program that retrieves book author details from an XML file when a button is clicked. The XML file stores book details. 3) A PHP script that creates a CD catalog using AJAX and data from an XML file storing CD details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
472 views8 pages

AJAX

The document describes three AJAX examples: 1) A PHP script that uses AJAX to validate a username and password from a database on login. 2) An AJAX program that retrieves book author details from an XML file when a button is clicked. The XML file stores book details. 3) A PHP script that creates a CD catalog using AJAX and data from an XML file storing CD details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Write a PHP script using AJAX concept, to check user name and password are valid or
Invalid (use database to store user name and password).
2. Write Ajax program to get book author details from XML file when user click on button.
Create XML file for storing details of book (title, author, year, price).
3. Write PHP script to create a CD catalog using XML file.(using Ajax)

1. Write a PHP script using AJAX concept, to check user name and password are valid or
Invalid (use database to store user name and password).

Login.html

<html>
<head>
<script type="text/javascript">
function validate()
{
uname=document.getElementById('t1').value;
pass=document.getElementById('t2').value;
}
</script>
</head>
<body>
<form action="Login.php" method=get>
<fieldset>
<legend> Enter the LogiInformn ation : </legend>
Enter UserName :<input type=text name=t1 id=uname><span id=a></span><br>
<p>Enter Password :&nbsp <input type=password name=t2 id=pass><br>
<div align="center">
<input type=SUBMIT value=SUBMIT onClick=validate()>
<div>
</form>
</body>
</html>

Login.php

<?php
$user1=$_GET['t1'];
$pass1=$_GET['t2'];
$hn="localhost";
$un="root";
$pass="";
$db="slipdb";
$link=mysqli_connect($hn,$un,$pass,$db);
if(!$link)
{
die('Connection Failed:'.mysqli_error());
}
$sql="select * from Login1";
$res=mysqli_query($link,$sql);
if(mysqli_num_rows($res)>0)
{
while($row=mysqli_fetch_assoc($res))
{
if((strcmp($user1,$row['User'])==0) && (strcmp($pass1,$row['Pass'])==0))
echo"Login Successful";
else
echo"Invalid User Name and Password";
}
}
else
{
echo"error";
}
mysqli_close($link);
?>

2. Write Ajax program to get book author details from XML file when user click on button.
Create XML file for storing details of book (title, author, year, price).

Books.xml

<?xml version="1.0" encoding="UTF-8"?>

<BOOK>
<COMPTER>
<TITLE>ABCDE</TITLE>
<AUTHOR>Jean</AUTHOR>
<ISBN>12345</ISBN>
<PUBLISHER>Penguin Random House India</PUBLISHER>
<PRICE>1000</PRICE>
<YEAR>2018</YEAR>
</COMPTER>

<COMPTER>

<TITLE>ABCDEFG</TITLE>

<AUTHOR>Steve</AUTHOR>

<ISBN>54123</ISBN>

<PUBLISHER>Rupa Publications</PUBLISHER>

<PRICE>1500</PRICE>

<YEAR>2018</YEAR>

</COMPTER>

<COMPTER>

<TITLE>ABCDEFGHIJ</TITLE>

<AUTHOR>Hennie</AUTHOR>

<ISBN>12354</ISBN>

<PUBLISHER>Jaico Publishing House</PUBLISHER>

<PRICE>2000</PRICE>

<YEAR>2018</YEAR>

</COMPTER>

</BOOK>

Ajaxtest2.php

<!DOCTYPE html>
<html>

<body>

<h2>List of Book Authors Name</h2>

<button type="button" onclick="booksnames()">

Click Here to List Books Authors Name</button>

<p id="p1"></p>

<script>

function booksnames() {

var obj = new XMLHttpRequest();

obj.open("GET", "books.xml", true);

obj.send();

obj.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {

callxmlfile(this);

} }; }

function callxmlfile(xml) {

var k,z,xmlcontent, result;

xmlcontent = xml.responseXML;

result = "";

z = xmlcontent.getElementsByTagName("AUTHOR");

for (k = 0; k< z.length; k++) {

result += z[k].childNodes[0].nodeValue + "<br>";

document.getElementById("p1").innerHTML = result;

}</script>

</body>

</html>
1. Write PHP script to create a CD catalog using XML file.(using Ajax)

Cd.html

<!DOCTYPE html>

<html>

<style>

table,th,td {

border : 1px solid black;

border-collapse: collapse;

th,td {

padding: 5px;

</style>

<body>

<h1>The XMLHttpRequest Object</h1>

<button type="button" onclick="loadDoc()">Get my CD collection</button>

<br><br>

<table id="demo"></table>

<script>

function loadDoc() {

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {

myFunction(this);

};

xhttp.open("GET", "cd_catalog.xml", true);

xhttp.send();
}

function myFunction(xml) {

var i;

var xmlDoc = xml.responseXML;

var table="<tr><th>Artist</th><th>Title</th></tr>";

var x = xmlDoc.getElementsByTagName("CD");

for (i = 0; i <x.length; i++) {

table += "<tr><td>" +

x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +

"</td><td>" +

x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +

"</td></tr>";

document.getElementById("demo").innerHTML = table;

</script>

</body>

</html>

cd_catalog.xml

<?xml version='1.0' encoding='UTF-8' ?>

<CATALOG>

<CD>

<TITLE>Empire Burlesque</TITLE>

<ARTIST>Bob Dylan</ARTIST>

<COUNTRY>USA</COUNTRY>

<COMPANY>Columbia</COMPANY>

<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>

</CD>

<CD>

<TITLE>Still got the blues</TITLE>

<ARTIST>Gary Moore</ARTIST>

<COUNTRY>UK</COUNTRY>

<COMPANY>Virgin records</COMPANY>

<PRICE>10.20</PRICE>

<YEAR>1990</YEAR>

</CD>

<CD>

<TITLE>Eros</TITLE>

<ARTIST>Eros Ramazzotti</ARTIST>

<COUNTRY>EU</COUNTRY>

<COMPANY>BMG</COMPANY>

<PRICE>9.90</PRICE>

<YEAR>1997</YEAR>

</CD>

<CD>

<TITLE>One night only</TITLE>

<ARTIST>Bee Gees</ARTIST>

<COUNTRY>UK</COUNTRY>

<COMPANY>Polydor</COMPANY>

<PRICE>10.90</PRICE>

<YEAR>1998</YEAR>

</CD>

<CD>

<TITLE>Sylvias Mother</TITLE>

<ARTIST>Dr.Hook</ARTIST>
<COUNTRY>UK</COUNTRY>

<COMPANY>CBS</COMPANY>

<PRICE>8.10</PRICE>

<YEAR>1973</YEAR>

</CD>

<CD>

<TITLE>Maggie May</TITLE>

<ARTIST>Rod Stewart</ARTIST>

<COUNTRY>UK</COUNTRY>

<COMPANY>Pickwick</COMPANY>

<PRICE>8.50</PRICE>

<YEAR>1990</YEAR>

</CD>

</CATALOG>

You might also like