当我使用自动完成脚本将PHP代码合并到HTML文件中时,请告知如何将生成的PHP查询中的数据生成到array[]
。
目前有2个正常工作的文件,这导致良好的下拉列表:
street.php
生成数据数组:
<?php
require_once ("pg_conn.php");
$qstreet = "SELECT
cmac.street.id,
cmac.street.street,
cmac.street.postcode,
cmac.street.id_dimos,
cmac.street.id_street
FROM
cmac.street";
$ress = pg_query ($qstreet);
while ( $row = pg_fetch_array ( $ress ) ) {
$array[]=$row[1];
}
if (!empty($_GET['term']))
{
$term = $_GET['term'];
$pattern = '/^'.preg_quote($term).'/iu';
echo json_encode(preg_grep($pattern, $array));
}
?>
和带有自动完成脚本的HTML文件:
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../../css/style.css" />
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/themes/sunny/jquery-ui.css">
<script>
$(function() {
$('#street').autocomplete({
source: 'street.php'
});
});
</script>
</head>
<body>
<table>
<tbody>
<form method="post">
<tr>
<td><label for="street">Street:</label></td>
<td><input type="text" placeholder="Street name" required="required" id="street" name="street" onchange="sendForm(this.form)"></td>
</tr>
-----------------------
</form>
</tbody>
</table>
</body>
</html>
当我将PHP代码合并到HTML文件中时,我希望从$array[]
获取数据作为源代码,并提供一些简单的解决方案:
$(function() {
$('#street').autocomplete({
source: ($pattern, $array)
});
});
答案 0 :(得分:0)
来自php.ru的@romach解决...也许有人会需要这个解决方案
$(function() {
$('#street').autocomplete({
source: <?= json_encode($array) ?>
});
});
&#13;