0

I am trying to make a search engine for my website however I am curious on how to make it auto-update i.e like google, facebook, etc. without having the page reload.

I am currently using MySQL with InnoDB tables and PHP. I know how to make the MySQL searches however does anyone have suggestions on how to produce the results in the search bar w/o reloading the page. I have done some research myself and nothing too useful has come up. Also, would fulltext using the MyISAM engine be more useful than searching by indexed rows.

I am still learning the ins and outs of web development so I prefer code suggestions as opposed to "link to this program and do this." That will come later once I know what I'm doing.

Thanks All

jlofft
  • 33
  • 1
  • 5

1 Answers1

0

You can use AJAX to accomplish this. You can write a small PHP script that makes a simple call to your database based on the characters currently entered into the search box, and then use AJAX to call it to populate your search box. I'm not going to write the entire thing out for you, because that's not what the site's for but you can start here. There's also a lot of tutorials online for stuff like this.

AJAX:

function getXmlHttpRequestObject() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        alert("Your browser does not support AJAX, please upgrade.");
    }
}

//Create XMLHTTPRequest Object
var searchEntry = getXmlHttpRequestObject();

//Send AJAX request, do something when it sends a response
function searchSuggest() {
    if (searchReq.readyState == 4 || searchReq.readyState == 0) {
            var str = document.getElementById('insertsearchboxidhere').value;
            searchEntry.open("GET", 'yourphpscript.php?search=' + str, true);
            searchEntry.onreadystatechange = doSearchStuff(); 
            searchEntry.send(null);
        }       
    }



//Function that does the work with the response
function doSearchStuff() {
    if (searchReq.readyState == 4) {
        var str = searchEntry.responseText;
            //Set innerHTML of your stuff to str, whatever else you want
    }
}

PHP: Obviously you'll need more than the code below, but the brunt of the work will be done by returning entries from your database that start with the letters currently entered into the searchbox. You can figure out the rest.

$query = "Select distinct(x) from (y) where x like '(z)%'";

x = field, y = table, z = variable you sent via AJAX

That should be more than enough to build an updating searchbox.

Johnnyoh
  • 369
  • 1
  • 7
  • thanks a lot I really appreciate your help. I do have a book with some Ajax in it but I havent really covered that section yet. And I do agree with "that's not what the site is for" because I really want to learn this well and not just copy paste the material. – jlofft Jul 31 '12 at 02:47