从文件中获取价值调用使用JSON Ajax请求价值、文件、Ajax、JSON

2023-09-10 20:39:37 作者:爱已归零

此文件被称为Ajax请求。而造成来到这里我想将它放在两个呼叫功能不同。

This file is called by ajax request. And result coming here I want to place into two different in calling function.

<?php


//Some processing gives $text 
$s=nl2br($text);

        $data['x'] = $p;
        $data['y'] = $q;

//Start from here

    echo "<b>Positive count : $x with $p % </b>";  echo "</br>";
    echo "<b>Negative count  : $y with $q % </b>"; echo "</br>";
    echo "</br>";
    echo "Page content : ";
    echo "</br>";
    echo "</br>";
    echo $s;
//End. This content should be place in <div1>. Want to send this as a json string

//Start from here
    echo "First 5 post";
    $result = mysqli_query($con,"select post from facebook_posts where p_id >  (select MAX(p_id) - 5 from facebook_posts)");
    while ($row = $result->fetch_array(MYSQLI_ASSOC))
    {
      echo $row['post'];
      echo '<br/>';
    }
//End. This content should be placed in <div2> Want to send this as a json string

如果有一个变量使用那么我们可以很容易地做到这一点:

If there is single variable then we can easily do it using :

$resultArray = array("resultOne" => $result1,"resultTwo" => $result2);
echo json_encode($resultArray);

在接收端:

document.getElementById("myFirstDiv").innerHTML=xmlhttp.responseText.resultOne;
document.getElementById("mySecondDiv").innerHTML=xmlhttp.responseText.resultTwo;

但是,如何通过上述复杂的结果可能是,投以JSON变量?

推荐答案

您可以使用的在PHP中的输出缓冲:

ob_start();
// Generate content for div 1...
$div1 = ob_get_clean();

ob_start();
// Generate content for div 2...
$div2 = ob_get_clean();

$result = array("div1" => $div1, "div2" => $div2);
echo json_encode($result);
 
精彩推荐
图片推荐