0

Hi All,

The following is a script ive found that lets me type something in and it reads from an array using ajax GET.

What im wanting to do is to not require a user to input anything but instead just immediatly call the information from the fixed value that i pass in.

Just like with php, i send a get parameter and it actions that, but i just dont want a click

<html>
<head>
<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;
    }
  }
xmlhttp.open("GET","gethint.asp?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<h3>Start typing a name in the input field below:</h3>
<form action=""> 
First name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p> 

</body>
</html>
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sergb
0
<html>
<head>
<script type="text/javascript">
function showHint(str)
{
  // Remove this because you want to allow empty string
  //if (str.length == 0)
  //{ 
  //  document.getElementById("txtHint").innerHTML = "";
  //  return;
  //}

  if (window.XMLHttpRequest)
    xmlhttp = new XMLHttpRequest();
  else
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
      document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
  };
  xmlhttp.open("GET", "gethint.asp?q=" + str, true);
  xmlhttp.send(null);
}
</script>
</head>
<body>

<h3>Start typing a name in the input field below:</h3>
<form action=""> 
First name: <input type="text" id="txt1" onkeyup="showHint(this.value);" />
</form>
<p>Suggestions: <span id="txtHint"></span></p> 

</body>
</html>

<script type="text/javascript">
// Send the first AJAX request when the page loads.
//
// Alternatively, you can add an onload attribute to body tag and call showHint
// function from there.
//   <body onload="showHint('');">

showHint("");
</script>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.