Start New Discussion within our Web Development Community

shouldn't this

while(($row=mysql_fetch_assoc($resource))!==false)

read in stead

while(($row=mysql_fetch_array($resource))!==false)

I am not posative on this, I am stil pretty new to php, however I have not seen the assoc function used before, have only ever seen the $row use the fetch array

for the 3 characters that have to be typed in so the jquery to launch and resource for that ?

All you have to do is check in the javascript code whether the search_term is at least 3 chars long. If not, you do not do an ajax call at all.

$(document).ready(function() {

    $('.autosuggest').keyup(function () {

        var search_term = $(this).attr('value');

        // if we have less than 3 chars just exit the function
        if(search_term.length < 3) {

            return;
        }

        ...

And also I made a small mistake in the html code, sory. I put the result unnumbered list (ul) at the end of the page. I later realized that it should be in the place where you put it originally, just after the input box, since it is the basis for extending the input box into combo box. So please move it back there.

<input type="text" name="tag" value="" placeholder="All" class="autosuggest" />
<div class="dropdown">
<ul class="result">
</ul>
...

so all i have to do is add this line of code to my js file and thats it?

 if(search_term.length < 3) {
return;
}

Yes, it basicaly exits teh function if there are less than 3 chars in the search_term. Haven't tested it, though, but I am pretty sure return statement works like exit in php. Other way of doing it would be wrapping the whole ajax bit in if(search_term.length >= 3).

$(document).ready(function() {

    $('.autosuggest').keyup(function () {

        var search_term = $(this).attr('value');

        if(search_term.length >= 3) {

            // read the values of the select elements (drop downs)

            // read the value of category 
            var category=$('#category').attr('value');

            // this is the conditions you will post over to the searchApp.php script
            // assembled here for clarity
            var conditions = {

                'search_term' : search_term,
                'category' : category
            }; 

            // alert(conditions.toSource());

            // post the conditions over to the searchApp.php script
            $.post('ajax/searchApp.php', conditions, function(data) {

                //alert(data);
                $('.result').html(data);

                /*
                // you can sort this out yourself
                $('.result li').click(function() {

                    var result_value=$(this).text();
                    $('.autosuggest').attr('value',result_value);
                    //alert(result_value);
                    $('.result').html('');

                });
                */

            });
        });
    });
}
<?php
require_once '../connect.php';
if(isset($_POST['search_term'])==true && empty($_POST['search_term'])==false)
{

$search_term=mysql_real_escape_string($_POST['search_term']);

$query = mysql_query("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`='{$_POST['category']}'` AND `Type` LIKE '$search_term'");

while(($row = mysql_fetch_assoc($query))!==false)
{
echo '<li>',$row['Type'],'</li>';
}
}
?> 

You should format your code neater.

are you sure all the Vars are case sensative etc.

try this

This question has already been answered. Start a new discussion instead.