AJAX,PHP DB查找

Currently I have a form and when the user is entering data, this code calls another page to check if the username is in use. This is the code from the main page that calls the DB Page(test.php)

<script type="text/javascript">
function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        if (xmlhttp.responseText.indexOf("green") != -1) {
            document.getElementById("submit").disabled = false;
        } else {
            document.getElementById("submit").disabled = true;
        }
    }
  }
xmlhttp.open("GET","test.php="+str,true);
xmlhttp.send();
}
</script>

I want to change this so when it calls test.php it passes a value to test.php based on what form field the user is entering into.

That way I can use test.php to perform diffident DB lookups depending on the form field the user is completing.

Can anyone help ? Thanks :)

An example with the Javascript jQuery ajax function. This function can be called whe user click on a component with id="myButton":

$(document).ready(function() {
    $('#myButton').click(function() {

        var myValue = $('#myInput').attr('value');

        var request = $.ajax({
            url: "test.php",
            type: "GET",
            data: {
                myInput : myValue
            },
            dataType: "json"
        });

        request.done(function(msg) {
            alert( "Request done: " + msg);
        });

        request.fail(function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
        });
    });
});

With this example you can take the value from a texfield with id = "myInput" and pass it with a GET call to the test.php page.

In the test.php page you can take the value simply with:

$_GET['myInput'];

Just one tip: your test.php must return a json type.