【发布时间】:2018-09-10 19:10:04
【问题描述】:
开始说我第一次接触 jquery 和 ajax 只是为了开发一个小网站来帮助我的工作社会提高生产力。 我一直在这里寻找答案,但似乎没有人遇到我同样的问题。 抱歉我的英语不好,我将向您展示我正在使用的代码,希望有人能提供帮助。
这里是 php。
<?php
try {
$dbh = new PDO ("mysql:host=localhost;port=33000;dbname=magazinoacc", 'root', 'administrator');
}
catch (PDOException $e) {
echo "Errore: " . $e->getMessage();
die();
}
// var_dump($_POST);exit;
if (! isset($_GET['term'])) {
$q="%";
} else {
$q=$_GET['term']."%";
}
$sql=" SELECT *
FROM pv
WHERE Cliente like :q;";
$stmt=$dbh->prepare($sql);
$stmt->bindparam(':q',$q);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stringa = [];
foreach($rows as $row){
$stringa[] = array(
'cliente' => $row['Cliente'],
'localita' => $row['Località'],
'indirizzo' => $row['Indirizzo'],
);
// $stringa.=sprintf('{cliente :" %s" localita" : "%s" indirizzo: "%s"},', $row['Cliente'], $row['Località'], $row['Indirizzo']);
}
// echo '['.substr($stringa,0,-1).']';
echo json_encode($stringa)
?>
这里是脚本
$(document).ready(function(){
$("#cliente").autocomplete({
minLength: 2,
source: function(request, response) {
$.ajax({
url:"ajax.php",
data: {
mode : "ajax",
term : request.term,
},
dataType: "json",
success: function(data) {
response(data);
console.log(data);
}
});
},
select: function(e, ui) {
$('#localita').val(ui.item.localita);
$('#indirizzo').val(ui.item.indirizzo);
}
});
});
这里是html
<table>
<form method="post">
<tr>
<td class="sx">value: </td>
<td class="dx"><input type="text" size="50" name="cliente" id="cliente">
</td>
</tr>
<tr>
<td class="sx">label: </td>
<td class="dx"><input type="text" size="50" name="localita" id="localita">
</td>
</tr>
<tr>
<td class="sx">label: </td>
<td class="dx"><input type="text" size="50" name="indirizzo" id="indirizzo">
</td>
</tr>
</form>
结果是这样的: image of the results
(无法显示更多细节) 如您所见,脚本返回了正确的 JSON 数组,但是在下拉列表中,我看不到任何内容,只有包含正确结果的黑色行。 为什么它们是空白的?
感谢您的建议
【问题讨论】:
-
如果你用这个怎么办:pastebin.com/RUMYzJNT?
-
如果发送的 json 格式为 - [{"value":"america"},{"value":"argentina"}]
-
@KarloKokkak 如果你看到图片,返回值与 [{"cliente" : ".....", "localita" : "....", "indirizzo" 完全一样: "....."}, ....] 事实上,如果我只使用 source: 'ajax.php' 结果是一样的,我可以用正确的结果填充其他字段
-
不是。它们被命名为 -
cliente、localita和indirizzo。他们应该被标记为"value:"。 -
是因为自动完成无法将标签识别为客户?
标签: php jquery ajax autocomplete jquery-ui-autocomplete