Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在不同的div中使用部分AJAX响应_Php_Ajax - Fatal编程技术网

Php 在不同的div中使用部分AJAX响应

Php 在不同的div中使用部分AJAX响应,php,ajax,Php,Ajax,我想发送此AJAX请求: function sumMonthly() { var cur_month = $('#month option:selected').attr("value"); var request = $.ajax({ type: "POST", url: 'inc/functions.php', data: {action: ["get_sum_income", "get_sum_expense", "get_cash_remaining"], cu

我想发送此AJAX请求:

function sumMonthly() {
var cur_month = $('#month option:selected').attr("value");

var request = $.ajax({
    type: "POST",
    url: 'inc/functions.php',
    data: {action: ["get_sum_income", "get_sum_expense", "get_cash_remaining"], cur_month:cur_month}
});
request.done(function(response){
    $('#sum_income').html('<h1 class="positiveNum float-right">&#36;' + response.get_sum_income + '</h1>');
    $('#sum_expense').html('<h1 class="float-right">&#36;' + response.get_sum_expense + '</h1>');
});
request.fail(function(jqxhr, textStatus){
    alert("Request failed: " + textStatus);
});
};
这不是你问题的真正“答案”,但我不能在评论中写下我想要的

您需要使用PHP来整合代码并生成如下数组:

$json = array
             (
               'varname1' => $variable1,
               'varname2' => $variable2,
               'varname3' => $variable3,
               'varname4' => $variable4,
               'varname5' => $variable5
              );
$jsonstring = json_encode($json);
echo $jsonstring;
然后在客户端,您可以执行以下操作:

.done(function(response) {
       $('#div1').text(response.varname1);
       $('#div2').text(response.varname2);
       $('#div3').text(response.varname3);
       $('#div4').text(response.varname4);
       $('#div5').text(response.varname5);
                          });
同样,这不是一个具体的解决方案,但它应该让您开始


我总是编写我的php,然后在没有客户端的情况下运行它,获取屏幕上的输出并提交给jsonlint.com,以确保它是json。

因此我假设$I是一个json数组。它是多行数组还是单行数组?如果是多行,则需要使用.each函数解析json数据。如果是单行,则可以只使用$('#divname').text(response.variablename);等等。也许你可以给我们提供更多的信息。不,这不是一个json数组。它是数组中的单个值。我需要使用json_编码吗?此外,上面的php代码只是多个代码块中的一个,所有代码块都回显$i,或者它们可能需要回显不同的变量,但问题是,当所有代码都说了和做了时,$i是一个数字,但ajax响应应该包括多个单独的数字。我想访问每个单独的数字,并将它们放在单独的div中。利用json_encode实现这一目的,这就是它存在的原因。我没有定义,如果我添加数据类型:“json”,我会得到一个解析错误。这非常有帮助。我将能够重构我的整个应用程序,使其更加高效。非常感谢。
.done(function(response) {
       $('#div1').text(response.varname1);
       $('#div2').text(response.varname2);
       $('#div3').text(response.varname3);
       $('#div4').text(response.varname4);
       $('#div5').text(response.varname5);
                          });