Janani
Janani

Reputation: 67

How to pull Php Json values as dropdown with Jquery?

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.

Upvotes: 0

Views: 175

Answers (1)

Ahmad Samilo
Ahmad Samilo

Reputation: 914

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>");
});

Upvotes: 1

Related Questions