supun rajasinghe
supun rajasinghe

Reputation: 121

I Can't select the search items

I'm creating search function using php,mysql,js and ajax. But i cant select the search items. I'm trying to select suggested words. there is load suggestions but i can't select them. here is the code i have created.please

this is index page. in index page script part is wrong or doesn't work the last part of fadeOut effect.

<head>
    <title>2nd year group project</title>
    <meta charset = "utf-8">
    <metaname="viewport" content="width=device-width, initial-scale=1">

    <!--this is for link css file-->
    <link rel="stylesheet" type="text/css" href="css/FindAProffesional.css">

    <!--//this is for link icons for site-->
    <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">

    <!--//this is for link bootstrap to site-->
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>  
<body>
<div style="width:500px;">
<input type="text" name="country" id="country" class="form-control" placeholder="Enter city name">
</div>
<div id="countryList"></div>
<script>
    $(document).ready(function(){
        $('#country').keyup(function(){
        var query = $(this).val();
        if(query!='')
        {
            $.ajax({
            url:"search.php",
            method:"POST",
            data:{query:query},
            success:function(data)
        {
        $('#countryList').fadeIn();
        $('#countryList').html(data);
        }
        });
        }   
    });

        $document().on('click','li',function(){
        $('#country').val($(this).text());
        $('#countryList').fadeOut();
        });
    });
</script>
</body>

this is serch.php page

<?php
    require('dbcon.php');
    if(isset($_POST["query"]))
    {
        $output='';
        $query ="SELECT DISTINCT city FROM architect WHERE city LIKE '%".$_POST["query"]."%'";
        $result = mysqli_query($conn,$query);
        $output = '<ul class="list-unstyled">';
        if(mysqli_num_rows($result) > 0)
        {
            while($row = mysqli_fetch_array($result))
            {
                $output .='<li>'.$row["city"].'</li>';
            }
        }
        else
        {
            $output .='<li>City not Found</li>';
        }
        $output .='</ul>';
        echo $output;

    }
?>

Upvotes: 1

Views: 61

Answers (1)

heXer
heXer

Reputation: 314

replace

$document().on('click','li',function(){
        $('#country').val($(this).text());
        $('#countryList').fadeOut();
        });

with

$(function(){
  $('#countryList').on('click','li',function(){
    $('#country').val($(this).text());
    $('#countryList').fadeOut();
  });
});

Upvotes: 2

Related Questions