使用jquery的Ajax相关查询

时间:2014-04-26 10:03:57

标签: javascript php jquery

我有一个html和php文件。

我使用d3在html文件和php文件中绘制ajax图表。

在HTML文件中,我有一个与此类似的功能来绘制d3图表

图表功能:

function draw_chart(reference, data) {
   // chart function.....   
}

AJAX SCRIPT:

        $.ajax({
            type: "POST",
            url: "data.php",
            data: { value:value },
            async: true,
            dataType: 'json'
        }).done(function(result) {
            div_name = '#chart_div';

            draw_chart(div_name, result);
        })

当然这很好用。

在PHP文件中,我已连接到PostgreSQL数据,我能够成功完成所有事情。

但是我想在图表中更新表格。

如果我在PHP文件中编写另一个查询并回显它。

我如何能够将结果分开并将其附加到表格?

根据我的知识,我只知道为表创建另一个PHP文件。

还有一个是在PHP文件中编写整个javascript函数,并使用jquery .html文件追加它。

但我想按照我上面所说的方式去做。是否有可能将其分离并将分离的数据附加到表中?

我实际上并不知道是否可以这样做,或者也可能早先被问过。

有人帮助我。

1 个答案:

答案 0 :(得分:0)

假设你在PHP中有这个:

//an array of results from the db you what to draw your charts with
 $my_results_array=get_from_db();

//then somewhere you encode it to json 
echo json_encode($my_result_array);

//then you output the text for the table
 echo $table_text;

我所说的是将$ table_text添加到数组的末尾并对其进行编码。

array_push($my_results_array,$table_text);
echo json_encode($my_results_array);

然后从JS中的JSON对象的最后一个位置获取文本。