jQuery UI autocomplete: how to send post data?

后端 未结 5 1995
伪装坚强ぢ
伪装坚强ぢ 2021-02-20 17:25

From jQuery UI site (veiw source):

$( \"#birds\" ).autocomplete({
    source: \"search.php\",
    minLength: 2,
    select: function( event, ui ) {
        log(          


        
5条回答
  •  生来不讨喜
    2021-02-20 18:04

    $( "#birds" ).autocomplete({ 
    source: function (request, response) {
        $.ajax({
      type: "POST",
      url:"search.php",
      data: request,
      success: response,
      dataType: 'json'
    });
      }
    }, {minLength: 3 });
    
    //-------------------------
    //search.php - example with request from DB
    
    //
    
    
     $link = mysql_connect($mysql_server, $mysql_login, $mysql_password)
            or die("Could not connect: " . mysql_error());
         mysql_select_db($mysql_database) or die("Could not select database");
         mysql_set_charset('utf8'); 
    
    $req = "SELECT mydata FROM $mysql_table WHERE mydata LIKE '".$_REQUEST['term']."%' ORDER BY mydata ASC";
    $query = mysql_query($req);
    
    while($row = mysql_fetch_array($query))
    {
        $results[] = array('label' => $row['mydata']);
    }
    
    
    echo json_encode($results);
    ?>
    

提交回复
热议问题