简体   繁体   English

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

[英]jQuery Autocomplete PHP Mysql posting different field

I am making use of jQuery's Autocomplete where I am populating my autocomplete dropdown with a php file called site.php. 我正在使用jQuery的自动完成功能,在其中使用名为site.php的php文件填充自动完成功能下拉列表。 Site.php gets the values from a mysql table called site and which has 3 columns: id , code and site . Site.php从名为site的mysql表中获取值,该表具有3列: idcodesite I want my autocomplete to show only code and site and then store the corresponding id in my other table. 我希望我的自动填充功能仅显示codesite ,然后将相应的id存储在其他表中。

Everything works fine except that autocomplete is posting the code and the site selected but not the id . 除了自动完成功能会发布code和选定的site而不是id之外,其他所有功能都可以正常运行。 What do I need to change in order to send the id to my php POST and not code and site ? 为了将id发送到我的php POST,而不发送codesite我需要更改什么? Scripts as follows: 脚本如下:

PHP file: site.php PHP文件: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: Javascript:

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

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

             $(this).autocomplete("search");
  });
});
</script>

In your PHP, change 在您的PHP中,更改

$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. “标签”属性将显示给用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM