我已經通讀了幾乎所有關於試圖獲取從PHP傳遞到javascript的數值的問題。正如你所看到的,我成功生成了隨機數並且效果很好,但是當我試圖將變量傳遞給graph.update函數時,即使兩個變量的值都根據螢火蟲(分別爲14060116和0,這是正確的)他們沒有把它更新到更新功能......任何想法?這是完整的腳本,如果它有幫助!變量不能從PHP傳遞到javascript
<script>
(function() {
function createCanvas(divName) {
var div = document.getElementById(divName);
var canvas = document.createElement('canvas');
div.appendChild(canvas);
if (typeof G_vmlCanvasManager != 'undefined') {
canvas = G_vmlCanvasManager.initElement(canvas);
}
var ctx = canvas.getContext("2d");
return ctx;
}
var ctx = createCanvas("graphDiv1");
var upld = <?php echo json_encode($dirupld[0]); ?>;
var upldred = <?php echo json_encode($dirupldred[0]); ?>;
var graph = new BarGraph(ctx);
graph.maxValue = 30;
graph.margin = 10;
graph.width = 450;
graph.height = 200;
graph.colors = ["#49a0d8", "#d353a0"];
graph.xAxisLabelArr = ["Traditional", "Duplicate Reduced"];
graph.update([upld, upldred]);
//graph.update([Math.random() * 30, Math.random() * 30]);
}());
</script>
不知道我失去了價值?螢火蟲是報告以下
var upld = 14060116;
var upldred = 0;
graph.update([upld, upldred]);
做嘗試把它們放在''「' – Deepak
如果你可以看到Firebug的變量,那不是純粹的JavaScript問題嗎? :) –
可以提供'json_encode($ dirupldred [0]);'從PHP腳本的示例輸出?它看起來像'upld'獲得一個整數,而不是JSON對象。 'upldred'也一樣 –