dongtu1357 2015-09-27 19:28
浏览 59

jQuery自动完成PHP Mysql发布不同的字段

I am making use of jQuery's Autocomplete where I am populating my autocomplete dropdown with a php file called site.php. Site.php gets the values from a mysql table called site and which has 3 columns: id, code and site. I want my autocomplete to show only code and site and then store the corresponding id in my other table.

Everything works fine except that autocomplete is posting the code and the site selected but not the id. What do I need to change in order to send the id to my php POST and not code and site? Scripts as follows:

PHP file: site.php

<?php
        $server = 'sql203.com';
        $user = 'xxxxxxxxxxxx';
        $password = 'xxxxxxx';
        $database = 'b17';
    $mysqli = new MySQLi($server,$user,$password,$database);
    /* Connect to database and set charset to UTF-8 */
    if($mysqli->connect_error) {
      echo 'Database connection failed...' . 'Error: ' . $mysqli->connect_errno . ' ' . $mysqli->connect_error;
      exit;
    } else {
      $mysqli->set_charset('utf8');
    }
    /* retrieve the search term that autocomplete sends */
    $term = trim(strip_tags($_GET['term'])); 
    $a_json = array();
    $a_json_row = array();
    if ($data = $mysqli->query("SELECT * FROM `b17_16413362_upupa`.`site` WHERE code LIKE '%$term%' OR site LIKE '%$term%' ORDER BY code , site")) {
        while($row = mysqli_fetch_array($data)) {
            $id = htmlentities(stripslashes($row['id']));
            $code = htmlentities(stripslashes($row['code']));
            $site = htmlentities(stripslashes($row['site']));

            $a_json_row["id"] = $id;
            $a_json_row["value"] = $code.' '.$site;
            $a_json_row["label"] = $code.' '.$site;
            array_push($a_json, $a_json_row);
        }
    }
    // jQuery wants JSON data

    echo json_encode($a_json);
    flush();

    $mysqli->close();
    ?>

Javascript:

<script type="text/javascript">
$(function() {

  $("#sitex").autocomplete({
    source: 'site.php',
     minLength: 0
        }).focus(function(){     

             $(this).autocomplete("search");
  });
});
</script>
  • 写回答

1条回答 默认 最新

  • duan3019 2015-09-27 19:40
    关注

    In your PHP, change

    $a_json_row["value"] = $code.' '.$site;
    

    to

    $a_json_row["value"] = $id;
    

    The 'value' property is the data that will be submitted by the form. The 'label' property is what will be displayed to the user.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器