在PHP中自动完成组合框

时间:2014-11-21 10:15:23

标签: php combobox autocomplete

我有一个带有填充信息的组合框(由mysql_query填充)我想添加自动完成功能。

$urun_bul="";

$sorgug = mysql_query("SELECT x FROM y order by z asc"); 

while( $yyy = mysql_fetch_array($sorgug))

{

$urun_bul.='<option value="'.$yyy["urun_id"].'">'.$yyy["urun_kodu"].' - '.$yyy["urun_adi"].'</option>';

}

然后打印$ urun_bul

1 个答案:

答案 0 :(得分:1)

试试吧 click here

您将使用此示例将您的选项标签与mysql绑定...

示例:

index.php文件

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title></title>
  <link rel="stylesheet" href="chosen.css">
  <style type="text/css" media="all">
    /* fix rtl for demo */
    .chosen-rtl .chosen-drop { left: -9000px; }
  </style>
</head>
<body>
  <form>
    <div id="container">
      <div id="content">

      <div class="side-by-side clearfix">
        <?php mysql_connect("hostname","username","password"); mysql_select_db("database_name"); ?>

        <div>
          <em>Select option with DB using autocomplete</em>
          <select data-placeholder="Choose a Country..." class="chosen-select" style="width:350px;" tabindex="2">
            <option value=""></option>
            <?php $sorgug = mysql_query("SELECT x FROM y order by z asc");
                while( $yyy = mysql_fetch_array($sorgug)){
            ?>
                <option value="<?php echo $yyy["urun_id"]; ?>"><?php echo $yyy["urun_kodu"].' - '.$yyy["urun_adi"]; ?></option>
            <?php } ?>
          </select>
        </div>
      </div>

    </div>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
  <script src="chosen.jquery.js" type="text/javascript"></script>
  <script type="text/javascript">
    var config = {
      '.chosen-select'           : {},
      '.chosen-select-deselect'  : {allow_single_deselect:true},
      '.chosen-select-no-single' : {disable_search_threshold:10},
      '.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
      '.chosen-select-width'     : {width:"95%"}
    }
    for (var selector in config) {
      $(selector).chosen(config[selector]);
    }
  </script>
  </form>

</body>
</html>

请从上面链接中下载的代码中复制 chosen.css chosen.jquery.js 文件,然后将其放入您的index.php文件所在的文件夹中。< / p>