Jump to content

Recommended Posts

Hi all,

 

I'm experimenting with Ajax and I'm kinda stuck. Underneath is my code please help me out.

 

<html>
<head>
<script type="text/javascript">
function showHint(str)
{
var xmlhttp;
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","ajax_test.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

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

</body>
</html>

<?php
// Fill up array with names
//Include database connection details
require_once('config.php');

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

//Connectie naar mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) 
{
    die('Failed to connect to server: ' . mysql_error());
}

//Selecteer database
$db = mysql_select_db(DB_DATABASE);
if(!$db) 
{
    die("Unable to select database");
}


        $query = "SELECT * 
                  FROM users
                  WHERE voornaam ='".$q."'";
        $result = mysql_query($query) or die('Query1 failed: ' . mysql_error());
        $totalRows = mysql_num_rows($result);
        $a = array();
        for ($j=0; $j<$totalRows; $j++) {
        $a[$j] = mysql_fetch_row($result);
        }


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

 

Link to comment
https://forums.phpfreaks.com/topic/238132-making-a-livesearch/
Share on other sites

Hi,

Sorry actually forgot ta ask the question.

When I type something in the textfield it should give me some suggestions from my db.

Somehow it keeps telling me there are no suggestions.

 

I think I'm doing something wrong in the call to the db but I tried different approaches and it still won't work.

Anyone have a clue?

Link to comment
https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1223661
Share on other sites

Maybe it helps to know that when I type in one of the names in de database which should be "predicted" it gives the following error:

Warning: mysql_fetch_row() expects parameter 1 to be resource, string given in /customers/ryflex.nl/ryflex.nl/httpd.www/ajax_test.php on line 31 no suggestion

When I put in 1 letter more or less it jumps back to no suggestion.

 

Link to comment
https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1224318
Share on other sites

Their $a is an array of strings. Your $a is an array of arrays. That's the biggest difference: mysql_fetch_row() returns an array of fields, not just one particular field. Instead try

$a[] = mysql_fetch_object($result)->predicted;

 

As for mysql_query() returning a string, I've never heard of that happening. What is your code?

Link to comment
https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1224341
Share on other sites

ok after modyfing the php file to what you told me  th query i run is now th following and I still don't get a result.

the error when I fill out an entire name is gone as well.

        $query = "SELECT * 
                  FROM users
                  WHERE voornaam ='".$q."'";
        $result = mysql_query($query) or die('Query1 failed: ' . mysql_error());
        $totalRows = mysql_num_rows($result);
      
    $a[] = mysql_fetch_object($result)->predicted;

Link to comment
https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1224488
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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