我已经阅读了几乎每个问题,试图获取从PHP传递给javascript的数值。正如你所看到的,我成功地生成了随机数,并且效果很好,但是当我尝试将变量传递给graph.update函数时,即使两个变量都有根据firebug的值(14060116,分别为0,这是正确的)他们没有进入更新功能...任何想法?如果它有帮助,这是完整的脚本!
<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>
不知道我在哪里丢失了价值观? Firebug正在报告以下内容
var upld = 14060116;
var upldred = 0;
graph.update([upld, upldred]);
答案 0 :(得分:0)
尝试更改graph.maxValue的值以显示upld
我假设你没有期待任何东西可以显示为upldred,因为它已经是0
答案 1 :(得分:0)
First you will passing hidden values in php form
something like this
<input type="hidden" name="upld" id="upld" value="<?php echo json_encode($dirupld[0]); ?>">
<input type="hidden" name="upldred" id="upldred" value=" <?php echo json_encode($dirupldred[0]); ?>">
then you will get this value in javascript
var upld=document.getElementById("upld").value;
var upldred=document.getElementById("upldred").value;
dont use below the code
var upld = <?php echo json_encode($dirupld[0]); ?>; //Is not Correct
var upldred = <?php echo json_encode($dirupldred[0]); ?>;//Is not correct
Use document.getElementById().value syntax
答案 2 :(得分:0)
你的PHP看起来很健康;你从PHP返回整数,然后将这些整数作为参数传递给条形图。正如其他人所提到的,它出现的事实意味着你的PHP很好,这是一个JS问题。
但是,当您设置graph.maxValue = 30;
时,当您尝试呈现值为14060116
的图表时,您会收到错误:
Uncaught IndexSizeError: Index or size was negative, or greater than the allowed value.
(我假设你正在使用http://www.williammalone.com/articles/html5-canvas-javascript-bar-graph/)