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.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP