[英]How to get php database values to javascript array
Hi here i am using the jquery auto-complete plugin. 您好,我在使用jquery自动完成插件。 i have some php retrieved database values i want to use those values in the autocomplete plugin.
我有一些php检索的数据库值,我想在自动完成插件中使用这些值。 for that i want to get the php values to javascript array.
为此,我想将php值获取到javascript数组。 how can i do this?
我怎样才能做到这一点?
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
];
$( "#category" ).autocomplete({
source: availableTags
});
});
Php: PHP的:
<?php
require_once "config.php";
$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select distinct(category) from completer";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
$fname = $rs['category'];
echo "$fname"
}
?>
Well, if you have the data store inside php variable, then you can add those values into javascript variable like so, no need for ajax stuff for this : 好吧,如果您在php变量中有数据存储,那么您可以像这样将这些值添加到javascript变量中, 而无需使用ajax的东西:
Supposed you have values inside php variable like : 假设您在php变量中具有以下值:
$myPhpVar = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
];
Then in your js part should be : 然后在您的js部分应该是:
$(function() {
var availableTags = [];
// start here - populate the data from php variable
<?php foreach($myPhpVar as $key => $val) {?>
availableTags.push('<?php echo $val;?>'); // push data into js variable
<?php }?>
// end here
$( "#category" ).autocomplete({
source: availableTags
});
});
JS script : JS脚本:
$(function() {
$.ajax({
type : 'get',
url : 'urlofmyfile.php',
data : 'q='+q,
dataType : 'json',
success : function(availableTags){
$( "#category" ).autocomplete({
source: availableTags
});
}
});
});
$.ajax documentation : http://api.jquery.com/jquery.ajax/ $ .ajax文档: http : //api.jquery.com/jquery.ajax/
PHP script : PHP脚本:
<?php
require_once "config.php";
$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select distinct(category) from completer";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
$fname[] = $rs['category'];
}
print json_encode($fname);
exit;
?>
json_encode documentation : http://php.net/manual/en/function.json-encode.php json_encode文档: http : //php.net/manual/zh/function.json-encode.php
There are several ways to achieve this. 有几种方法可以实现此目的。
Dynamic page generation. 动态页面生成。
You can dynamically get values from database and insert into the HTML as JSON object: 您可以动态地从数据库获取值,并将其作为JSON对象插入HTML:
$(function() {
var availableTags = <?php
require_once "config.php";
$q = strtolower($_GET["q"]);
if (!$q) die("[]");
$sql = "select distinct(category) from completer";
$rsd = mysql_query($sql);
$row = array();
while($rs = mysql_fetch_assoc($rsd)) {
$row[] = $rs['category'];
}
echo json_encode($row);
?>;
$("#category").autocomplete({
source: availableTags
});
});
AJAX. AJAX。
AJAX approach is almost the same but it is considered to be a better practice. AJAX方法几乎相同,但被认为是更好的做法。 In this case, PHP file is a separate file which returns only an object handled by JS.
在这种情况下,PHP文件是一个单独的文件,仅返回由JS处理的对象。
PHP: PHP:
<?php
require_once "config.php";
$q = strtolower($_GET["q"]);
if (!$q) die("[]");
$sql = "select distinct(category) from completer";
$rsd = mysql_query($sql);
$row = array();
while($rs = mysql_fetch_assoc($rsd)) {
$row[] = $rs['category'];
}
echo json_encode($row);
?>
JS: JS:
$(function() {
$.ajax({
url: 'yourPhp.php',
data: 'q=' + q,
dataType: 'json'
}).done(function(dt) {
$("#category").autocomplete({
source: dt
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.