将php数据库结果传递给javascript数组 [英] Passing php database result to javascript array

查看:23
本文介绍了将php数据库结果传递给javascript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个下拉列表和一个按钮,点击后将添加另一个下拉列表.我使用了这个 javascript 代码:

I have to create a dropdown list and a button that will add another dropdownlist once click. i used this javascript code:

var array = ["Volvo","Saab","Mercades","Audi"];

        var cell2 = row.insertCell(1);
        var element2 = document.createElement("select");
        element2.name="activity_dropdown"
        element2.id="activity_dropdown"
        cell2.appendChild(element2);

        for (var i = 0; i < array.length; i++) {
            var option = document.createElement("option");
            option.setAttribute("value", array[i]);
            option.text = array[i];
            element2.appendChild(option);
        }

但我希望我的 var 数组来自数据库结果查询.我该怎么做才能将 php 数组传递给 javascript 数组?

but i want that my var array will come from the database result query. what can i do to pass php array to javascript array?

顺便说一句..我试过使用json_encode但它似乎不起作用..这是我的代码:

by the way.. i have tried to used json_encode but it seems not working.. here is the my code:

用于 data.php

for data.php

    <?php
    include('connection/dbConn.php');


// get data and store in a json array
$activity=$conn->query("SELECT activity_name FROM rehab_activities");


while ($row =mysqli_fetch_array($activity)) {
    $activities[] = array(
    'actvityName' => $row['activity_name']
    );
}

echo json_encode($activities);
 ?>

并在这里尝试,但没有任何结果:

and try it here, but nothing comes out:

<script>
 jQuery(document).ready(function(){
    jQuery("#add").click(function(){
    var res = new Array(); 
        jQuery.getJSON("data.php", function(data) {
    var i= 0;
    while(i<data.myarray.length){
                res[i]=data.myarray[i];
             i++;
    }
  jQuery("#result").html(res[0]);
            });
        });

    });
</script>
<body>
    <input type="button" id="add">
    <div id="result"></div>
</body>
</html>

推荐答案

php 编写脚本 的那部分,同时循环遍历您的查询.像这样:

Make php write that part of your script while it loops through your query. Like so:

var array = [
    <?php
        $query = mysql_query("SELECT * FROM cars");
        while ($car = mysql_fetch_assoc($query)) {
            $car_name = $car["name"];
            echo "'$car_name',";
        }
    ?>
];

为此,您的 file 必须是 php 文件而不是 javascript 文件,因为在 php 文件中,您还可以编写 html,因此编写 javascript.

For this to work, your file must be a php file and not a javascript file, since inside a php file you can also write html and therefore, javascript.

我个人通过制作一个名为 db-to-js.php 的文件来解决这个问题,该文件只包含 javascript 数组 由 php 创建.每当数据库修改时,这些javascript数组中的一个或多个也会被修改,因为它们每次执行此文件时都会创建.

I personally solve this by making a file called db-to-js.php that only contains javascript arrays created by php. Whenever the database is modified, one or more of these javascript arrays are also modified since they are created every time this file is executed.

最后,您必须将文件作为脚本包含:

Lastly, you have to include the file where you need it as a script:

<script type="text/javascript" src="db-to-js.php"></script>

我希望这会有所帮助.

这篇关于将php数据库结果传递给javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆