2012-05-24 57 views
2

我還有一個問題:Ajax表單返回變量到php

Ajax表單運行良好。他們大多數需要做的東西,只有返回值,如果該條目可以寫或不。我只用了回聲。例如回聲「1」;如果可以寫入值並回顯「2」;如果這些值不能被寫入。

現在我需要回調3個變量。我知道我可以將它們寫入一個數組中。我的問題是,我不能將這個變量返回到我的可見網站。

這是我的JavaScript代碼:

//Show statistic 
$('.statistic_submit').click(function(){ 
    if ($('#month').val() == 'none' || $('#year').val() == 'none') { 
     $("#dialog_empty").dialog("open"); 
     return false; 
    } 

    var form = $('#statistic_view'); 
    var data = form.serialize(); 

    $.ajax({ 
     url: "include/scripts/user_statistic.php", 
     type: "POST", 
     data: data, 
     success: function (reqCode) { 
      if (reqCode == 1) { 
       //Show generated table 
       $('.done').fadeOut('slow'); 
       $('.done').fadeIn('slow'); 
      } 
      if (reqCode == 2) { 
       //No values found 
       $('.done').fadeOut('slow'); 
       $("#dialog_error").dialog("open"); 
      } 
     } 
    }); 
    return false; 
}); 

這是我的html代碼:

<div> 
    <form id="statistic_view" action="include/scripts/user_statistic.php" method="post"> 
     <select name="month" id="month"> 
      <option value="none" class="bold italic">Monat</option> 
      <?php 
       for($i=1; $i<=12; $i++){ 
        if($i == $month) 
         echo "<option value=\"".$i."\" selected>".$month_name[$i]."</option>\n"; 
        else 
         echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n"; 
       } 
      ?> 
     </select> 
     <select name="year" id="year"> 
      <option value="none" class="bold italic">Jahr</option> 
      <?php 
       for($i=2012; $i<=$year; $i++){ 
        if($i == $year) 
         echo "<option value=\"".$i."\" selected>".$i."</option>\n"; 
        else 
         echo "<option value=\"".$i."\">".$i."</option>\n"; 
       } 
      ?> 
     </select> 
     <br/><br/> 
     <div id="user_statistic"> 
      <input type="submit" id="small" class="statistic_submit" value="Daten anzeigen"> 
     </div> 
    </form> 
    <br /> 
    <div class="done"> 
     <p class="bold center"><?php echo "Besucher ".$month_name[$month]." ".$year; ?></p> 
     <canvas id="cvs" width="680" height="250">[No canvas support]</canvas> 
     <script> 
      chart = new RGraph.Line('cvs', <?php print($data_string) ?>); 
      chart.Set('chart.tooltips', <?php print($labels_tooltip) ?>); 
      chart.Set('chart.tooltips.effect', 'expand'); 
      chart.Set('chart.background.grid.autofit', true); 
      chart.Set('chart.gutter.left', 35); 
      chart.Set('chart.gutter.right', 5); 
      chart.Set('chart.hmargin', 10); 
      chart.Set('chart.tickmarks', 'circle'); 
      chart.Set('chart.labels', <?php print($labels_string) ?>);   
      chart.Draw(); 
     </script> 
    </div> 
</div> 

這我user_statistic.php:

... (mysql stuff) 
    /******************************/ 
    /** Create diagram 
    /******************************/ 
    $labels = array(); 
    $data = array(); 

    for ($j=1; $j<=$days; $j++) { 
     $labels[$j] =$j; 
     $data[$j] = $day_value[$j]; 
    } 

    // Aggregate all the data into one string 
    $data_string = "[" . join(", ", $data) . "]"; 
    $labels_string = "['" . join("', '", $labels) . "']"; 
    $labels_tooltip = "['" . join("', '", $data) . "']"; 

    //data written 
    echo "1"; 

所以回聲 「1」;告訴我的腳本,一切都很好。但現在我需要$ data_string,$ labels_string和$ labels_tooltip。那麼我怎麼能從user_statistic.php中返回這些值到我的身邊呢?

回答

2

避免將數組自己轉換爲字符串。如果你需要傳遞一個PHP數組回你的jQuery,你應該用json_encode功能做到這一點:

echo json_encode($array); 

這會通過一個JSON對象,然後您可以處理客戶端。您的JSON字符串將被返回到您的$.ajax方法的回調:

$.ajax({ 
    url: "include/scripts/user_statistic.php", 
    type: "POST", 
    data: data, 
    dataType: 'json', 
    success: function (response) { 
    /* response is your array, in JSON form */ 
    } 
}); 

舉例來說,如果我們的PHP腳本做了以下內容:

$response = array(
    'message' => 'Success', 
    'allData' => array('Jonathan', 'Mariah', 'Samuel', 'Sally') 
); 

echo json_encode($response); 

我們可以從我們的jQuery提醒message是這樣的:

success: function (response) { 
    alert(response.message); 
} 
+0

不要忘記使用'dataType'爲'json'。或者在服務器端設置標題,指定響應是json。 –

+0

如何在PHP中處理json響應?因爲必須將變量寫入.done容器以顯示圖表。 – JPM

+0

@JPM你不會在* PHP中處理JSON響應*。 PHP *向JavaScript發送* JSON響應。 JavaScript處理JSON數據。我在回答的底部向您展示瞭如何訪問JSON對象的成員。 – Sampson

1

這裏最好的方法是返回一個json對象。在服務器端創建一個數組 -

$response['error_code'] = '1'; //everything ok. 0 if not ok 
$response['data_string'] = 'this will have some data'; 
$response['labels_string'] = 'labels'; 
$response['labels_tooltip' = 'here goes the tooltips'; 
echo json_encode($response); 

,並在你的JavaScript代碼,更不用說返回的數據類型爲JSON -

$.ajax({ 
    url: "include/scripts/user_statistic.php", 
    type: "POST", 
    data: data, 
    dataType: json, 
    success: function (reqCode) { 
     if (reqCode.error_code == 1) { 
      alert('this is the data string '+resCode.data_string); 
      //Show generated table 
      $('.done').fadeOut('slow'); 
      $('.done').fadeIn('slow'); 
     } 
     if (reqCode.error_code == 2) { 
      //No values found 
      $('.done').fadeOut('slow'); 
      $("#dialog_error").dialog("open"); 
     } 
    } 
});