0% found this document useful (0 votes)
14 views

Ajax Practical

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Ajax Practical

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

//SetA1

//contact.dat

1 Isha 65768798 9876543200 Shirur


2 Megha 65235689 8765432921 Pune
3 Ganesh 40768705 7876543276 Mumbai

//setA1.html

<html>
<head>
<title>Ajax</title>
<style>
span
{
font-size: 25px;
}
table
{
color: blueviolet; ;
}
</style>

<script type="text/javascript" >

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>";

while($row = fscanf($fp, "%s %s %s %s %s"))


{
echo "<tr>";
foreach($row as $r)
{
echo "<td>$r</td>";
}
echo "</tr>";
}
echo "</table>";
fclose($fp);
?>

//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.

teacher_details=# create table teacher(tno int, tname varchar(30),


qualification varchar(40), salary float, constraint pk primary key(tno));
CREATE TABLE
teacher_details=# insert into teacher values(1, 'mr.patil', 'bsc.comp.sci.',
76543);
INSERT 0 1
teacher_details=# insert into teacher values(2, 'mr.yadav', 'ba', 16543);
INSERT 0 1
teacher_details=# insert into teacher values(3, 'mr.jadhav', 'bcom', 116543);
INSERT 0 1
teacher_details=# insert into teacher values(4, 'mr.padwal', 'bsc.chemistry',
24543);
INSERT 0 1
teacher_details=# insert into teacher values(5, 'mr.wable', 'bsc.botany',
20543);
INSERT 0 1
teacher_details=# select * from teacher;
tno | tname | qualification | salary
-----+-----------+---------------+--------
1 | mr.patil | bsc.comp.sci. | 76543
2 | mr.yadav | ba | 16543
3 | mr.jadhav | bcom | 116543
4 | mr.padwal | bsc.chemistry | 24543
5 | mr.wable | bsc.botany | 20543
(5 rows)

//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'];

$con = pg_connect("host=localhost port=5432 dbname=teacher_details


user=postgres password=postgres");

$sql = "select * from teacher where tname='$t_name';";

$result=pg_query($con,$sql);

echo "<table border=1>";


echo "<tr>";
echo "<th>Teacher No.</th>";
echo "<th>Teacher Name</th>";
echo "<th>Qualification</th>";
echo "<th>Salary</th>";
echo "</tr>";

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.

customer_order=# create table customer(cust_no int, cust_name varchar(20),


city varchar(30), constraint pk primary key(cust_no));
CREATE TABLE

customer_order=# create table orders(order_no int,order_date


date,shipping_address varchar(30), cust_no int,constraint pk1 primary
key(order_no),constraint fk foreign key(cust_no) references
customer(cust_no));
CREATE TABLE

customer_order=# insert into customer values(1,'ganesh','mumbai');


INSERT 0 1
customer_order=# insert into customer values(2,'geeta','pune');
INSERT 0 1
customer_order=# insert into customer values(3,'mukesh','shirur');
INSERT 0 1
customer_order=# insert into customer values(4,'prakash','hydrabad');
INSERT 0 1
customer_order=# insert into customer values(5,'prajakta','nagar');
INSERT 0 1

customer_order=# insert into orders values(10,'04-12-2021','nagar',2);


INSERT 0 1
customer_order=# insert into orders values(20,'10-14-2022','shirur',3);
INSERT 0 1
customer_order=# insert into orders values(30,'03-10-2020','hydrabad',1);
INSERT 0 1
customer_order=# insert into orders values(40,'07-12-2020','mumbai',4);
INSERT 0 1
customer_order=# insert into orders values(50,'05-14-2022','pune',5);
INSERT 0 1

customer_order=# select * from customer;


cust_no | cust_name | city
---------+-----------+----------
1 | ganesh | mumbai
2 | geeta | pune
3 | mukesh | shirur
4 | prakash | hydrabad
5 | prajakta | nagar
(5 rows)

customer_order=# select * from orders;


order_no | order_date | shipping_address | cust_no
----------+------------+------------------+---------
10 | 2021-04-12 | nagar | 2
20 | 2022-10-14 | shirur | 3
30 | 2020-03-10 | hydrabad | 1
40 | 2020-07-12 | mumbai | 4
50 | 2022-05-14 | pune | 5
(5 rows)

//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'];

$con = pg_connect("host=localhost port=5432 dbname=customer_order


user=postgres password=postgres");

$sql = "select cust_name,order_no,order_date,shipping_address from


customer as c, orders as o where c.cust_no=o.cust_no and
cust_name='$cust_name';";

$result = pg_query($con, $sql);


echo "<table border=1>";
echo "<tr>";
echo "<th>Customer Name</th>";
echo "<th>Order No</th>";
echo "<th>Order Date</th>";
echo "<th>Shipping Address</th>";
echo "</tr>";

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 ;
?>

You might also like