Autocomplete Textbox results based from SQL database

前端 未结 5 985
情深已故
情深已故 2020-12-16 06:16

I\'m trying to create an auto-complete function into a textbox but the result should come from my SQL database.

Here\'s the code that i\'m trying to configure:
<

5条回答
  •  暖寄归人
    2020-12-16 06:43

    Your autocomplete.php file,

    include('conn.php');
     $sql="SELECT * FROM oldemp";
     $result = mysqli_query($mysqli,$sql) or die(mysqli_error());
    
    //Create an array
    $arr = Array();
    while($row=mysqli_fetch_array($result))
    {
        array_push($arr,$row['name']);
    }
    header('Content-Type: application/json');
    echo json_encode($arr)
    ?>
    

    The result of this will be an JSON array which can be directly used in JavaScript. Hence, the script will be something like -

    var availableTags = [];
    $.ajax({
        url:"autocomplete.php",success:function(result){
        availableTags = result
    }});
    

提交回复
热议问题