Ajax Practical
Ajax Practical
//contact.dat
//setA1.html
<html>
<head>
<title>Ajax</title>
<style>
span
{
font-size: 25px;
}
table
{
color: blueviolet; ;
}
</style>
function print()
{
var obj=false;
obj=new XMLHttpRequest();
obj.open("POST","ajax_setA1.php");//emailid="+eid);
obj.send();
obj.onreadystatechange=function()
{
if(obj.readyState==4 && obj.status==200)
{
document.getElementById("i").innerHTML=obj.responseText;
}
}
}
</script>
</head>
<body>
<center><h3>Display the contents of a contact.dat file </h3>
<br><input type="button" value="Print" onclick="print()" >
<br><br><span id="i"></span>
</center>
</body>
</html>
//setA1.php
<?php
$fp = fopen('contact.dat','r');
echo "<table border=1>";
echo "<tr>
<th>Sr.No.</th>
<th>Name</th>
<th>Residence No.</th>
<th>Mobile No.</th>
<th>Address</th>
</tr>";
//SetB1
//teacher_details database
[ctbora@localhost html]$ su
Password:
[root@localhost html]# service postgresql start
Redirecting to /bin/systemctl start postgresql.service
[root@localhost html]# su postgres
bash-4.4$ creatdb teacher_details
bash: creatdb: command not found
bash-4.4$ createdb teacher_details;
bash-4.4$ psql teacher_details;
psql (10.19)
Type "help" for help.
//setB1.html
<html>
<head>
<title>AJAX</title>
<script type="text/javascript">
function display()
{
name=form1.name1.value;
if(window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","ajax_setB1.php?
name1="+name,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('res').innerHTML =
xmlhttp.responseText;
}
}
}
</script>
<body>
<form name="form1">
<b>Enter Teacher Name :</b><input type="text"
name="name1"/><br>
<input type="button" value="Submit" onclick="display()"/>
</form>
<div id='res'></div>
</body>
</html>
//setB1.php
<?php
$t_name = $_REQUEST['name1'];
$result=pg_query($con,$sql);
while($row = pg_fetch_row($result))
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
}
echo "</tr>";
echo "</table>";
?>
//SetB2
//customer_order database
[ctbora@localhost html]$ su
Password:
[root@localhost html]# su postgres
bash-4.4$ createdb customer_order;
bash-4.4$ psql customer_order;
psql (10.19)
Type "help" for help.
//setB2.html
<html>
<head>
<title>AJAX</title>
<script type="text/javascript">
function display()
{
name=form1.name1.value;
if(window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","ajax_setB2.php?
name1="+name,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('res').innerHTML =
xmlhttp.responseText;
}
}
}
</script>
<body>
<form name="form1">
<b>Enter Customer Name :</b><input type="text"
name="name1"/><br>
<input type="button" value="Print Details" onclick="display()"/>
</form>
<div id='res'></div>
</body>
</html>
//setB2.php
<?php
$cust_name = $_REQUEST['name1'];
while($row = pg_fetch_row($result))
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
}
echo "</tr>";
echo "</table>";
?>
//SetC1
//setC1.html
<html>
<head>
<title>AJAX</title>
<script type="text/javascript">
function showHint(str)
{
if (str.length == 0)
{
document.getElementById("txtHint").innerHTML = "";
return;
}
else
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("txtHint").innerHTML =
xmlhttp.responseText;
}
};
xmlhttp.open("POST", "ajax_setC1.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form>
First Name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Name: <span id="txtHint"></span></p>
</body>
</html>
//setC1.php
<?php
$q = $_REQUEST["q"];
$hint = "";
if ($q !== "")
{
$hint = $q;
}
echo $hint ;
?>