我尝试从javascript数组函数
加载自动填充文本框plz建议我如何在基于php pdo sql的javascript函数中获取数据。
我需要从sql database..plz加载这个标签,建议我怎么做......
这是我的PHP代码。
<?php
$book_no2 = $load_book["book_no"];
$receipt = $database->getRows("SELECT DISTINCT receipt_no FROM scheme_master WHERE book_no2 = :book_no2", array(':book_no2'=>$book_no2));
?>
我需要在$ receipt结果集上面加载以下availableTags .... plz帮助我
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
答案 0 :(得分:0)
如果javascript在php页面内,请尝试此操作:
<script>
$(function() {
var availableTags = [
<?php foreach($receipt AS $r=>$value){
echo '"'.$value.'"';
if($r != count($receipt)-1) { echo ", ".PHP_EOL; }
}?>
];
</script>
答案 1 :(得分:0)
你的意思是:
<?php
$book_no2 = $load_book["book_no"];
$query = "SELECT DISTINCT receipt_no
FROM scheme_master
WHERE book_no2 = :book_no2";
$receipt = $database->getRows($query, array(':book_no2' => $book_no2));
// create tag list
$tags = '';
foreach ($receipt AS $row) {
$tags .= '"'.$row->receipt_no.'",'.PHP_EOL;
}
$tags = substr($tags,0,-2);
// write javascript
echo "<script>
$(function() {
var availableTags = [
$tags
];
</script>";
?>
答案 2 :(得分:0)
您可以创建一个外部PHP文件并将其调用到jQuery ajax函数。所以你可以得到那些标签
例如
$.ajax({
type: 'GET',
url: 'tags.php',
success: function(data) {
var exploded = data.split("|");
...
}
});
在php文件中
<?php
$book_no2 = $load_book["book_no"];
$receipt = $database->getRows("SELECT DISTINCT receipt_no FROM scheme_master WHERE book_no2 = :book_no2", array(':book_no2'=>$book_no2));
$output = '';
foreach($receipt as $rec){
$output .= $receipt->receipt_no.'|'; // Since I'm not familiar with PDO SQL, I'm not sure of this syntax
}
echo $output
?>
这是一个非常粗略的例子,还有许多其他方法来处理数据。例如,我更喜欢使用JSON,我使用json_encode函数来回显PHP中的数据。使用jsonparse函数从jquery读取它。
希望有所帮助