hey everyone,
i got a website that works with ajax, well simply what it does is that when you start typing customers name it shows the hint about what names you could be trying to spell.
i want to change this so it wouldnt show the name sugestion but it would show customer type such as golden customer or silver or bronze. while im writing their name.
here is the code that i got so far.

index.html

<html>
<head>
<script src="clienthint.js"></script>
</head>
<body>

<form>
First Name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p>

</body>
</html>

clienthint.js

var xmlhttp

function showHint(str)
{
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
var url="gethint.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  }
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

and gethint.php

<?php
// Fill up array with names and type
$a[]="Anna -silver"; // what i want when the name is                              
$a[]="Brittany-silver";//being typed it would not show the name 
$a[]="Cinderella-gold";// it would only show the type
$a[]="Diana-bronze";


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

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
  {
  $hint="";
  for($i=0; $i<count($a); $i++)
    {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      {
      if ($hint=="")
        {
        $hint=$a[$i];
        }
      else
        {
        $hint=$hint." , ".$a[$i];
        }
      }
    }
  }

// 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;
?>

thanks guys i hope you can help me sort this out
thanks in advance;)

Recommended Answers

All 2 Replies

Try this:

<?php
// Fill up array with names and type
$a[]="Anna -silver"; // what i want when the name is                              
$a[]="Brittany-silver";//being typed it would not show the name 
$a[]="Cinderella-gold";// it would only show the type
$a[]="Diana-bronze";


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

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
  {
  $hint="";
  foreach($a AS $b)
    {
    if (strtolower($q)==strtolower(substr($b,0,strlen($q))))
      {
      $var=$b;
      $var=explode('-',$var);
      $var=$var[(count($var)-1)];
      if ($hint=="")
        {
        $hint=$var;
        }
      else
        {
        $hint=$hint." , ".$var;
        }
      }
    }
  }

// 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;
?>

thanks a lot this really works
appreciate your help
saved me hours of work
thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.