0

I am doing a Autocomplete function using php with jquery.On Entering first letter in alert box it is displaying values correctly.I need to get the values as dropdownlist.

My coding is:

<html>
<head>
<script>
$(document).ready(function(){
    $("#localty").keyup(function(){
        var localty = $("#localty").val();
        $.ajax({
            type : "POST",
            url: "getlist.php",
            dataType : "json",
            data : "localty="+localty,
            success: function(result){
                alert(result);
            }
        });
    });
});
</script>
</head>
<body>
<input type="text" id="localty">
<input type="submit" name="submit" value="Submit">
</body>
</html>

getlist.php page:

    <?php
    $localty = $_REQUEST['localty'];


    $con = mysqli_connect('localhost','root','','details');
    if (!$con) 
    {
    die('Could not connect: ' . mysqli_error($con));
     }
     mysqli_select_db($con,"details");
     ?>

    <?php
    $sql="SELECT localty FROM localty WHERE localty like '$localty%'";
    $result = mysqli_query($con,$sql);
    while($row = mysqli_fetch_array($result))
    {
        $row_set[] = $row['localty'];
    }
    echo json_encode($row_set);
    mysqli_close($con);
    ?>

Please give any Suggestions.

1 Answer 1

1

Your code have to be like this:

Your HTML

<select id="myselect">
</select>

The javascript(Using jQuery)

var json={} // Populate this json object
$.each(json, function(key, value){
    $('#myselect').append("<option value='"+key+"'>"+value+"</option>");
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.