JQuery从数据库多个字段自动完成

时间:2018-01-12 16:46:04

标签: javascript php jquery sql database

我正在尝试调整此代码以处理数据库中的另一个字段。我是JQuery的新手,任何帮助都会受到赞赏。

如何从PHP / SQL中获取$tableName以通过JQuery与#tableName输入字段连接?其他一切都在发挥作用。谢谢!

JQuery的:

<script>
    $(function() {
        function log( message ) {
            $("#compId").val(message);
            $( "#compId" ).scrollTop( 0 );
        }
        $( "#company" ).autocomplete({
            source: "/autoComp/ALL/agents.php",
            minLength: 2,//search after two characters
            select: function( event, ui ) {
                log( ui.item ? ui.item.id : "");
            }
        });
    });
    $("#company").on("click", function() {
        if ($(this).val() == "search")
            $(this).val("")
    });
</script>

PHP来源:

include "../../agents/inc/DBconnect.php";

$term = $_GET['term'];//retrieve the search term that autocomplete sends
$theQuery = "SELECT company AS value, compId AS id, 'agencies' AS tableName FROM agencies2 WHERE company LIKE '%" . $term . "%'
    UNION
    SELECT CONCAT(fName, ' ', IFNULL(lName, '')) AS value, staffId AS id, 'agtStaff' AS tableName FROM staff2 WHERE fName LIKE '%" . $term . "%' OR lName LIKE '%" . $term . "%'";

$result = $mysql->query($theQuery);

unset($row_set);

for ($i = 0; $i < $result->num_rows; $i++) {

    $result->data_seek($i);

    $aRow = $result->fetch_assoc();

    $aRow['value'] = stripslashes($aRow['value']);
    $aRow['id'] = stripslashes($aRow['id']);
    $tableName = stripslashes($aRow['tableName']);

    $row_set[] = $aRow; //build an array
}
echo json_encode($row_set);//format the array into json data

$result->free();
$mysql->close();

HTML:

<div><input type="radio" id="searchType" name="searchType" value="agents" onclick="setAutoComp('/autoComp/ALL/agents.php')"/>Agents</div>
<div><input type="radio" id="searchType" name="searchType" value="managers" onclick="setAutoComp('/autoComp/ALL/managers.php')"/>Managers</div>
<div><input type="radio" id="searchType" name="searchType" value="CDs" onclick="setAutoComp('/autoComp/ALL/CDs.php')" />   Casting Directors</div>

<input type="hidden" id="compId" name="compId" />
<input type="hidden" id="tableName" name="tableName" />
<input type="text" name="company" id="company">&nbsp;&nbsp;&nbsp;
<button>Search</button>

0 个答案:

没有答案