Ajax and PHP file with MySQL connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
idy
Forum Newbie
Posts: 12
Joined: Mon Sep 11, 2006 10:12 am

Ajax and PHP file with MySQL connection

Post by idy »

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>
PHP file below :[/syntax]

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]
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Your PHP code doesn't seem to connect to the database, is this only a part of it?
$keyword is not sanitized.
idy
Forum Newbie
Posts: 12
Joined: Mon Sep 11, 2006 10:12 am

Post by idy »

I only removed the connection parameters to MySQL from the PHP file, but I do assure you that the PHP file works correctly when tested independently.
Btw, what do you exactly mean by 'sanitized' ?
Thanks !
idy
Forum Newbie
Posts: 12
Joined: Mon Sep 11, 2006 10:12 am

Post by idy »

My idea behind all this :

My table contains a column "type" (fruit / vegetable) and a "product" column (apple / orange / etc.) for instance.

If the user searches for "fruit", then all the fruits should be displayed : classical database query.

Then, if the user unchecks an "apple" checkbox for instance, I would like that the fruit list automatically refreshes without the whole page being reloaded... ajax in short.

Thanks for your help again.
DrTom
Forum Commoner
Posts: 60
Joined: Wed Aug 02, 2006 8:40 am
Location: Las Vegas

Post by DrTom »

First, "var fichier_externe = "db_request.php?word=" + keyword;" will not resolve to anything. You have to use a valid url. So http://www.mydomain.com/db_request.php?word=keyword.
idy
Forum Newbie
Posts: 12
Joined: Mon Sep 11, 2006 10:12 am

Post by idy »

Thanks for your contribution but it did not solve anything.
Btw, if the PHP file is in the same directory, is it still absolutely necessary to enter an absolute path rather than a relative one ?
Thanks again.
DrTom
Forum Commoner
Posts: 60
Joined: Wed Aug 02, 2006 8:40 am
Location: Las Vegas

Post by DrTom »

It's a regular HTTP request executed from the client. It's the same as if they ahd typed it into the location bar. So yes, it has to be a completely valid url for it to work. And yeah I was looking at it some more and I'm gettin some very strange errors in firefox. Will look into a bit mroe in just a sec.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Yep, and also try
http://www.mydomain.com/db_request.php?word=don't use unescaped strings in a database
idy
Forum Newbie
Posts: 12
Joined: Mon Sep 11, 2006 10:12 am

Post by idy »

True that I get strange errors in the JS debugging tool of Firefox :
Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: FICHIER.HTML :: anonymous :: line 35" data: no]
Source File: FICHIER.HTML
Line: 35
Did not find anything very relevant in the forums online, although it seems my purpose is quite common (query retrieval and ajax functionality of refreshing only part of an HTML page).

Thanks a lot for your active help.
DrTom
Forum Commoner
Posts: 60
Joined: Wed Aug 02, 2006 8:40 am
Location: Las Vegas

Post by DrTom »

Okay I looked into it a bit more and figured out some things
Apparently firefox doesn't like the XMLHttpRequest.status things, so yeah I'm not sure what thats about
but hte reason it doesn't work is pretty darn simple

Code: Select all

<input type="submit" id="ok" value="OK" onclick="search()">
should be

Code: Select all

<input type="button" id="ok" value="OK" onclick="search()">
Since it was a submit button and there was no action it was basically callign the ajax request then refreshing the page after that and overwriting the changes made from the ajax request.
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

i've recently run into the NS_ERROR_NOT_AVAILABLE problem but it was due to a bug in the ff XMLHttpRequest object when using popup windows. in your case it might be something similar. here's the bug writup: https://bugzilla.mozilla.org/show_bug.cgi?id=317600 the workaround at the bottom fixed it for me.
idy
Forum Newbie
Posts: 12
Joined: Mon Sep 11, 2006 10:12 am

Post by idy »

Thanks to all of you, Mordred, Rooster242 and especially Dr.Tom whose answer solved this issue.
Post Reply