PHP and AJAX Search Form

Hi, i am trying to get this script to work correctly, but am not having much luck. :frowning: I have been trying to change this tutorial to integrate with my database.

The problem i have is when it creates a link to the result it is only creating a link for the first item only. Any help would be appreciated. :slight_smile: Also i have nearly 27,000 entries in the table bb1_dad_roll would this cause a problem for the webhost if i used a script like this online?

This is my gethint.php file:


<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("roh", $con);

$result = mysql_query("SELECT * FROM bb1_dad_roll");

while($row = mysql_fetch_array($result))
  {
$id = $row['roll_id'];
$surname = $row['roll_surname'];
$forename = $row['roll_forename'];
$b[] = $id;
$a[] = $surname;
$c[] = $forename;
}
mysql_close($con);

//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="<br /><a href=\\"test.php?id=$b[$i]\\">".$a[$i]." ".$b[$i]."</a><br />";
		}
      else
        {
		$hint = "<a href=\\"test.php?id=$b[$i]\\">$hint $a[$i] , $c[$i] $b[$i]</a><br />";
        }
      }
    }
  }

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

This is my HTML file (seems to work ok):


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

you should just add a counter variable like this to insert elements in array:
<?php

$con = mysql_connect(“localhost”,“root”,“”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“roh”, $con);

$result = mysql_query(“SELECT * FROM bb1_dad_roll”);
$i=0;
while($row = mysql_fetch_array($result))
{
$id = $row[‘roll_id’];
$surname = $row[‘roll_surname’];
$forename = $row[‘roll_forename’];
$b[$i] = $id;
$a[$i] = $surname;
$c[$i] = $forename;
$i++;
}
mysql_close($con);

//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=“<br /><a href=\“test.php?id=$b[$i]\”>”.$a[$i]." “.$b[$i].”</a><br />";
}
else
{
$hint = “<a href=\“test.php?id=$b[$i]\”>$hint $a[$i] , $c[$i] $b[$i]</a><br />”;
}
}
}
}

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