Skip to content

Commit bb4207b

Browse files
author
root
committed
Initial commit with working files
0 parents  commit bb4207b

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

callback.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
Header('Content-type: text/xml');
3+
$title = '';
4+
$author = '';
5+
if(isset($_GET["title"])) $title = $_GET["title"];
6+
if(isset($_GET["author"])) $author = $_GET["author"];
7+
$title = pg_escape_string($title);
8+
$author = pg_escape_string($author);
9+
10+
$db_conn = pg_connect("host=localhost user=pi password=a") or die("cannot connect to db");
11+
$db_query = "select query_to_xml(E'select * from book where author like \'%".$author."%\' and title like \'%".$title."%\'', false, true, '')";
12+
$result = pg_query($db_conn, $db_query);
13+
if(!$result) {
14+
echo "An error occured";
15+
pg_close($db_conn);
16+
exit;
17+
}
18+
echo '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n";
19+
echo "<books>\n";
20+
while($row = pg_fetch_row($result)) {
21+
echo $row[0];
22+
}
23+
echo "</books>";
24+
pg_close($db_conn);
25+
?>
26+

index.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title> XML Demo </title>
6+
<style>
7+
#resultTable {
8+
border-collapse: collapse;
9+
}
10+
11+
#resultTable, #resultTable th, #resultTable td {
12+
border: 1px solid black;
13+
}
14+
15+
form td {
16+
text-align: right;
17+
}
18+
</style>
19+
<script>
20+
//AJAX script
21+
function callback(form) {
22+
var table; //the html to insert
23+
var book; //the list of books returned
24+
var subElement; // a subelement of a book
25+
var i; //loop counter
26+
var http; //the http request
27+
var params = "title="+form.elements["title"].value+"&author="+form.elements["author"].value;
28+
http = new XMLHttpRequest();
29+
http.open("GET", "callback.php?"+params, true);
30+
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
31+
http.setRequestHeader("Content-length", params.length);
32+
http.setRequestHeader("Connection", "close");
33+
http.onreadystatechange = function() {
34+
if(http.readyState==4 && http.status==200) {
35+
table="\n<table id='resultTable'>\n<tr><th>Title</th><th>Author</th></tr>\n";
36+
book=http.responseXML.documentElement.getElementsByTagName("row");
37+
for(i=0; i<book.length; i++) {
38+
table+="<tr>";
39+
subElement=book[i].getElementsByTagName("title");
40+
try {
41+
table+="<td>"+subElement[0].firstChild.nodeValue+"</td>";
42+
} catch (e) { //in case this ^ doesn't exist
43+
table+="<td></td>";
44+
}
45+
subElement=book[i].getElementsByTagName("author");
46+
try {
47+
table+="<td>"+subElement[0].firstChild.nodeValue+"</td>";
48+
} catch (e) {
49+
table+="<td></td>";
50+
}
51+
table+="</tr>\n";
52+
}
53+
table+="</table>\n";
54+
document.getElementById("result").innerHTML=table;
55+
}
56+
}
57+
http.send();
58+
59+
}
60+
</script>
61+
</head>
62+
<body>
63+
<h1>Your Local Library's Online Electronic Card Catalog</h1>
64+
<i>Early Alpha (v0.0.0.1)</i><br><br>
65+
66+
<form onsubmit="callback(this);return false;">
67+
<table>
68+
<tr><td>Title:</td><td><input type="text" name="title"></td></tr>
69+
<tr><td>Author:</td><td><input type="text" name="author"></td></tr>
70+
</table>
71+
<input type="submit" value="Search!">
72+
</form>
73+
74+
<br>
75+
<div id="result">
76+
</div>
77+
78+
</body>
79+
</html>

0 commit comments

Comments
 (0)