2016-01-12 21 views
1

是有可能得到一個單頁結果2個JSON輸出通過將數據傳遞到另一個頁面設置是有可能得到兩種不同的JSON輸出

我有數據填充網格和圖表AJAX並獲得牽引類型JSON的結果從單一MySQL查詢設置,當我嘗試返回JSON未辦理

這裏是我的result.php代碼將在其中生成JSON,

include('connection.php'); 
if (isset($_POST)) { 
    $rep_date1 = $_POST['date1']; 
    $date1 = date("Y-m-d", strtotime($rep_date1)); 
    $rep_date2 = $_POST['date2']; 
    $date2 = date("Y-m-d", strtotime($rep_date2)); 
    $sql = mysql_query("SELECT * FROM infra.prob_report WHERE prob_rept_date BETWEEN '$date1' AND '$date2'"); 
    $rows = array(); 
    while ($row = mysql_fetch_assoc($sql)) { 
     $nestedData = array(); 
     $nestedData[] = $row["rep_id"]; 
     $nestedData[] = $row["prob_rept_date"]; 
     $nestedData[] = $row["prob_equip_name"]; 
     $nestedData[] = $row["prob_rept_name"]; 
     $nestedData[] = $row["prob_desc"]; 
     $data[] = '<tr><td>'.$row["rep_id"]. 
     '</td><td>'.$row["prob_rept_date"]. 
     '</td><td>'.$row["prob_equip_name"]. 
     '</td><td>'.$row["prob_rept_name"]. 
     '</td><td>'.$row["prob_desc"]. 
     '</td></tr>'; 
     $point = array("label" => $row['prob_equip_name'], "y" => $row['rep_id']); 
     array_push($data_points, $point); 
    } 
    echo json_encode($data); //json output populating data-grid 
    echo json_encode($data_points); //json output populating chart 
} 

這裏是我的handle.php在我的處理腳本運行,

<script> 
    $(document).ready(function() { 

     $('#submit').click(function() { 
      var name = $("#name").val(); 
      event.preventDefault(); 
      $.ajax({ 
       type: "POST", 
       url: "new_prob_submit.php", 
       data: { 
        'date1': $('#picker1').val(), 
        'date2': $('#picker2').val() 
       }, 
       dataType: "json", 
       success: function(data) { 
        $('#tbdy').html(data); 
        $.getJSON("result.php", function(result) { 
         var chart = new CanvasJS.Chart("chartContainer", { 
          data: [{ 
           dataPoints: result 
          }] 
         }); 
         chart.render(); 
        }); 
       } 
      }); 
     }); 
    }); 
</script> 
+0

可以'push'都在一個'array'然後'echo'最後輸出數組。所以你有'result [0]'來填充數據網格和'result [1]'來填充圖表。 –

+0

@ParthTrivedi你可否請我詳細說明一下,因爲我必須同時使用同一組元素 –

+0

同時呈現圖表和數據網格。 –

回答

1

只要做一兩件事。

在PHP

echo json_encode(
    array(
     'data' => $data, 
     'dataPoints' => $data_points 
    ) 
); //json output populating data-grid and populating chart 

在javascript中

success:function(result){ 
result.data; 
result.dataPoints; 
} 
+0

非常感謝很多傢伙的工作很好,現在確實很棒! –

+0

@Balaji你發現任何有用的答案,然後請標記'真實'的標誌。因此,其他人可以通過'真實'符號快速獲得幫助。謝謝! :) –

相關問題