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!
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>
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]
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 !
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 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.
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.
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).
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
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.
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.