Jump to content

Search Database for particular ID


Dysan

Recommended Posts

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>

Link to comment
Share on other sites

well your probably going to at least have to create a hidden input field to record the key strokes. you well need to set up your php page to search your database; do something like this:

 

<?php

// connect
// select db
$hiddenfieldvariable = $_GET['hiddenfieldname'];
$results = mysql_query("select * from where id like '$hiddenfieldvariable%'");
// then go from there

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.