Ajax and PHP file with MySQL connection
Posted: Mon Sep 11, 2006 10:14 am
feyd | Please use
PHP file below :[/syntax]
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hello to you all !
I would appreciate your help with the piece of code I have written below, the first file written in Ajax allowing a user to search any keyword which is then sought for in a MySQL table through a PHP file. The PHP file and MySQL connection should be OK, as I have tested them independently.
When I run the first file, nothing seems to happen, not even the "No results found" message from the PHP file.
Thanks a lot for your help.
Ajax file below :
[syntax="html"]
<html>
<head>
<script language = "javascript">
function search()
{
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp");
}
if(XMLHttpRequestObject) {
var keyword = document.getElementById('text_zone').value;
var fichier_externe = "db_request.php?word=" + keyword;
XMLHttpRequestObject.open("GET", fichier_externe);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
var affichage = XMLHttpRequestObject.responseText;
document.getElementById('target').innerHTML = affichage;
delete XMLHttpRequestObject;
XMLHttpRequestObject = null;
}
}
XMLHttpRequestObject.send(null);
}
}
</script>
</head>
<form>
<input type="text" id="text_zone" size="40">
<input type="submit" id="ok" value="OK" onclick="search()">
</form>
<br><br>
<div id="target">
</div>
</html>
Code: Select all
<?
if(!$_GET["word"]) {
echo "No results found";
}
else {
$keyword = $_GET["word"];
$query = "select * from $table where a='$keyword' ";
$result = mysql_db_query($db,$query,$link);
echo
"
<table>
";
while ($array = mysql_fetch_object($result)) {
echo
"
<tr>
<td>
$array->a
</td>
<td>
$array->b
</td>
</tr>
";
}
echo
"
</table>
";
}
?>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]