Jump to content

Need info about textfield / dropdown


xwishmasterx

Recommended Posts

I am looking for some info on creating a textfield /dropdown menu that works just like a standard browser: when you type in something in the textfield/dropdown it should show results (this is needed to quickly find a member)

 

eg. I type "use" and dropdown should contain members(results) starting with "use" (user1, user2 ...)

 

Anyone know where I can find info on this?

Link to comment
https://forums.phpfreaks.com/topic/235330-need-info-about-textfield-dropdown/
Share on other sites

I don't know if this code is 100% correct but here goes...

 

 

The HMTL page:

 

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

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

</body>
</html>

 

The PHP page: (I think the while loop needs to be different)

 

<?php
$sql = mysql_query("SELECT name from members");

//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
  {
  $hint="";
  while($result == mysql_fetch_array($sql))
    {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      {
      if ($hint=="")
        {
        $hint=$result[0];
        }
      else
        {
        $hint=$hint." , ".$result[0];
        }
      }
    }
  }

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.