2012-05-24 49 views
1
... 
success: function (reqCode) { 
      if (reqCode['error_code'] == 1) { 
       //Generiere Tabelle  
       $(".done").html( 
        '<p class="bold center"><?php echo "Besucher ".$month_name[' + reqCode['month'] + ']." ".' + reqCode['year'] + '; ?></p>' 
        '<canvas id="cvs" width="680" height="250">[No canvas support]</canvas>' 
        '<script>' 
         'chart = new RGraph.Line("cvs", ' + reqCode['data_string'] + ');' 
         'chart.Set("chart.tooltips", ' + reqCode['labels_string'] + ');' 
         '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", ' + $reqCode['labels_tooltip'] + ');' 
         'chart.Draw();' 
        '</script>' 
       );  
       $('.done').fadeOut('slow'); 
       $('.done').fadeIn('slow'); 
      } 
} 

我不知道爲什麼每一個新行都需要自己的'..'。無論如何,這是行不通的。看着API參考,但沒有發現任何有用:(.html()幫助不知道如何處理它

編輯:對於我的第二個問題:

這是JSON響應:

$response['error_code'] = '1'; 
    $response['data_string'] = "[" . join(", ", $data) . "]"; 
    $response['labels_string'] = "['" . join("', '", $labels) . "']"; 
    $response['labels_tooltip'] = "['" . join("', '", $data) . "']"; 
    $response['month'] = $month_name[$month]; 
    $response['year'] = $year; 

    echo json_encode($response); 
+0

你需要把字符串加在一起。但更重要的是,你不能在客戶端使用'php' ... –

+0

@Gaby aka G. Petrioli:php標籤將在它到達客戶端之前呈現。 – Naor

+0

@Naor,如果你注意到他正在嘗試從ajax調用返回一個值給'php'腳本。 –

回答

2

有似乎是有點不妥<script>標籤,但真的嗎?你不需要插入那個<script>標籤。您已經在運行JavaScript;只是這樣做:

success: function (reqCode) { 
    if (reqCode['error_code'] == 1) { 
     var month_name = <?php echo json_encode($month_name); ?>; 
     //Generiere Tabelle  
     $(".done").html( 
      '<p class="bold center">Besucher ' + month_name[reqCode['month']] + ' ' + reqCode['year'] + '</p>'+ 
      '<canvas id="cvs" width="680" height="250">[No canvas support]</canvas>' 
     ); 

     var chart = new RGraph.Line("cvs", reqCode['data_string']); 
     chart.Set("chart.tooltips", reqCode['labels_string']); 
     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", reqCode['labels_tooltip']); 
     chart.Draw(); 

     $('.done').fadeOut('slow'); 
     $('.done').fadeIn('slow'); 
    } 
} 

我已經修復了一些語法錯誤,雖然我不能保證沒有剩下。只需觀看JavaScript控制檯的錯誤。

+0

正如其他人在評論中提到的那樣,'<?php'塊中的'$ month_name'位根本不起作用;你必須將你的整個'$ month_name'數組複製到一個JavaScript數組中(例如'var month_name = <?php echo json_encode($ month_name);?>;',並將JavaScript數組用於JavaScript操作。可能還會修復它在我的答案 – Brilliand

+0

是啊我只是把腳本標籤放入.html()原因.done是容器應該繪製的圖表 – JPM

+0

謝謝這是正確的答案:)沒關係在哪裏我稱之爲chart.Draw();修正了PHP的東西了。現在我只使用json變量。 – JPM

1

你應該有一流的容器元素上它做,如:

<div class="done" /> 

此外,您可以做空:

$('.done').html('..all the html..').fadeOut('slow').fadeIn('slow'); 

而且,由於Vivin Paliath說,你應該使用+

'<a>'+ 
'asdsad'+ 
'</a>' 

好運Concat的在HTML中的所有字符串!

0

您需要將單個字符串傳遞給html()函數。例如:

$(".done").html("<p>hello</p><p>goodbye</p>")

如果需要多個字符串相結合,你將需要使用字符串連接,如:你需要+跡象

var combinedString = '<p>hello</p>' + '<p>goodbye</p>' 
1


success: function (reqCode) { 
      if (reqCode['error_code'] == 1) { 
       //Generiere Tabelle  
       $(".done").html('<p class="bold center"></p>'+ 
        '<canvas id="cvs" width="680" height="250">[No canvas support]'+ 
        '<script>'+ 
         'chart = new RGraph.Line("cvs", ' + reqCode['data_string'] + ');'+ 
         'chart.Set("chart.tooltips", ' + reqCode['labels_string'] + ');'+ 
         '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", ' + $reqCode['labels_tooltip'] + ');'+ 
         'chart.Draw();'+ 
        '' 
       );  
       $('.done').fadeOut('slow'); 
       $('.done').fadeIn('slow'); 
      } 
}