High guyz am tryin to get hint from my database but i am managing to do so,though if i list the array am able. please help if any one has an idea

here is the html code

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

and the php code looks like this

<?php
// Fill up array with names
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";

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

Recommended Answers

All 6 Replies

hehe your ajax example is from w3schools...

ok first make a database for names, then query them all

then changed this part from an array

// Fill up array with names
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";

to

$sql = mysql_query("SELECT name FROM users"); //sample table users
while($row=mysql_fetch_array($sql)){
$a[] = $row['name'];
}

First of all please rephrase your question.

Secondly, I recommend to use jQuery instead of reinventing the wheel again in using ajax.

Thirdly, i believe that w3schools ajax code is not correct since it was not working with me under some circumstances. Change the line 27 from

xmlhttp.send(); to xmlhttp.send(NULL);

@vaultdweller123 thanx the code worked...and yes code is from W3schools.
@shubhamjain1 the code for w3schools works alright but you need to understand it, in this case if you changed the last part of the code

xmlhttp.send();

to

xmlhttp.send(arguments);

it will work under all circumstances..thanx anyway

@vaultdweller123 tha code worked for a few minutes when i came back it gave me the following error:
mysql_fetch_array() expects parameter 1 to be resource, boolean. the warning is shown at the following line

while($row=mysql_fetch_array($query, $db) or die(mysql_error))

just stick to my original code and you'll do fine

$sql = mysql_query("SELECT name FROM users"); //sample table users
while($row=mysql_fetch_array($sql)){
$a[] = $row['name'];
}

thanx ur code helped me alot tho it didn't work as surposed so i changed some part of code and it worked..i changed it to

$query = 'SELECT name FROM ecomm_products ORDER BY product_code';
$result= mysql_query($query, $db) or die(mysql_error($db));
while($row =mysql_fetch_assoc($result)){
    foreach($row as $value){
     $a[] = $row['name'];
    }
}

thanx anyway

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.