gpt4 book ai didi

javascript - 如何远程使用 javascript 自动完成和 php

转载 作者:行者123 更新时间:2023-11-28 07:12:01 25 4
gpt4 key购买 nike

我正在尝试创建一个具有自动完成功能的简单搜索表单。自动完成的值取自不同服务器上的数据库。我有这个:

<form action="smth.php" method="POST">
<label>Tracking Tag</label>
<input class="form-control" type="text" name="tag" id="tag" />
<button class="submit btn btn-success" type="submit" name="search" value="Show">Show</button>
</form>

我的小 JavaScript 是:

$("#tag").autocomplete({source: "suggest_tag.php", minLength:1});

现在我有了这个被调用的 suggest_tag.php 文件。

<?php
if ( !isset($_REQUEST['term']) )
exit;

$data = file_get_contents("http://xxx/suggest_tag.php? tag=".$_REQUEST["term"]);
echo $data;
?>

http/xxx/suggest_tag.php中的脚本如下:

<?php
if ( !isset($_GET['tag']) )
exit;

$conn = mysql_connect(**credentials**) or die('Not able to connect to db');
$query = "select query;";
$result = mysql_query($query) or die(mysql_error());

$data = array();
if ( $result && mysql_num_rows($result) )
{
while( $row = mysql_fetch_array($result, MYSQL_ASSOC) )
{
$data[] = array("label"=>$row['tag'], "value"=>$row["tag"]);
}
}
echo json_encode($data);
?>

我做错了什么以及如何解决它?

最佳答案

您正在使用具有自动完成功能的自定义数据。如果您这样做,则需要覆盖 selectfocus 函数,如 the documentation 中所述。 .

关于javascript - 如何远程使用 javascript 自动完成和 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31205919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com