Click to See Complete Forum and Search --> : Search Database for particular ID


Dysan
01-28-2008, 06:41 PM
Using Ajax and the following code, how do I enable the user to type directly onto a page, without using a text box. Upon each character being entered, how do I search a database for the complete string?

If the string corresponds to a id in the database, how do I display a message, notifying the user that a record was found?

The following code is what I have so far. This code enables the user to type directly onto a page. How do I add Ajax to the following code, in order to check the ID field in the database for the string entered onto the page?

<script type='text/javascript'>
var x=null;
var y=null;
var tId = "";
window.onload = function() {
x = document.getElementById('x');
y = document.forms[0].copyField;
document.onkeypress = myOnKeyPress;
}
function myOnKeyPress(e) {
e = (e)?e:window.event;
key = (e.keyCode)? e.keyCode: e.which;
if (key==27) {
x.innerHTML =""; // escape pressed to clear the field
y.value =""; // remove if you do not want to clear field
}
else {
x.innerHTML += String.fromCharCode(key);
y.value = x.innerHTML;
}
}
</script>

bpat1434
01-28-2008, 09:15 PM
Well, you would really need a "timeout" between character typing which defines the end of a word/phrase. Or perhaps a specific key or combo which is the delimiter.

Then, just take that string (x.innerHTML) and send off an AJAX query (there are plenty of libraries or frameworks to use, or you can roll your own). Then your PHP would do the query and either send back the HTML, a JSON object, or some XML. Then your javascript would take that response, grab the proper text (depending upon the response (JSON, XML, Text, HTML)) and then populate whatever <div> you want.