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>

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;)

Dani AI

Generated

Brief follow-up for : 's answer correctly extracts the customer type instead of returning full name entries. For a more robust, maintainable solution consider returning structured JSON, de-duplicating types server-side, and reducing client requests with a short debounce. That approach is easier to parse on the client, safer to insert into the DOM, and scales when you switch from an array to a database.

Example server-side pattern (PHP) — map names to types, match case-insensitively, remove duplicates and send JSON:

<?php
$data = ['Anna'=>'silver','Brittany'=>'silver','Cinderella'=>'gold','Diana'=>'bronze'];
$q = trim($_GET['q'] ?? '');
$types = [];
if ($q !== '') {
  foreach ($data as $name => $type) {
    if (stripos($name, $q) === 0) $types[] = $type;
  }
  $types = array_values(array_unique($types));
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($types);
?>

Client-side: debounce input, fetch JSON, then safely insert textContent (not innerHTML) to avoid XSS:

let t;
input.addEventListener('input', () => {
  clearTimeout(t);
  t = setTimeout(async () => {
    const res = await fetch('/get-types.php?q=' + encodeURIComponent(input.value));
    const types = await res.json();
    document.getElementById('txtHint').textContent = types.join(', ');
  }, 250);
});

Notes and cautions: if you use a database, query with DISTINCT and a prepared statement (PDO) to avoid duplicates and SQL injection. When names may include non-ASCII characters use multibyte-safe functions (mb_*). For Fetch examples see Using Fetch and for JSON encoding in PHP see json_encode.

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.