简体   繁体   English

将php变量传递给jQuery flot piechart

[英]Passing php Variables to jQuery flot piechart

I have 3 variables defined in php 我在PHP中定义了3个变量

$record = "283-161-151";
$rec = explode("-", $record);
$win = $rec[0];
$draw = $rec[1];
$loss = $rec[2];

$win, $draw and $loss echo fine, so I know they are working $ win,$ draw和$ loss回波很好,所以我知道它们在起作用

When setting the var data in a separate js file which runs at the foot of the page, I want to pass the 3 variables to 3 set text labels. 当在页面底部运行的单独的js文件中设置var数据时,我想将3个变量传递给3个设置的文本标签。

var data = [
{ label: 'Wins',  data: '<?php echo $win ?>', color:'green'},
{ label: 'Draws',  data: '<?php echo $draw ?>', color:'orange'},
{ label: 'Losses',  data: '<?php echo $loss ?>', color:'red'}
];

If I change each of the js php calls from <?php echo $win ?> to the actual number for testing the chart shows. 如果我将每个js php调用从<?php echo $win ?>更改为测试图表的实际数字,则显示为。

I wonder if the inline php syntax is correct? 我想知道内联php语法是否正确? I have tried a couple of different formats, hoping its an easy fix with a fresh pair of eyes. 我尝试了几种不同的格式,希望能用一双新鲜的眼睛轻松解决。

Update 更新资料

php code PHP代码

<?php
$record = "283-161-151";
$rec = explode("-", $record);
?>
<script>
 var win = <?php echo $rec[0]; ?>;
 var draw = <?php echo $rec[1]; ?>;
 var loss = <?php echo $rec[2]; ?>;
</script>

js file code js文件代码

var data = [
{ label: 'Wins',  data: var win, color:'green'},
{ label: 'Draws',  data: var draw, color:'orange'},
{ label: 'Losses',  data: var loss, color:'red'}
];

When I look at page source I see this 当我查看页面源代码时,我看到了

<script>
 var win = 283;
 var draw = 161;
 var loss = 151;
</script>

But still no output.. if I replace var win, var draw etc in the final js file with numbers it does work.. 但是仍然没有输出..如果我用数字替换最终js文件中的var win,var draw等,它确实起作用。

anyone know if I am getting close? 有人知道我是否接近吗? Cheers 干杯

I think the problem is that you are quoting the numbers. 我认为问题是您在引用数字。 Change it so that the numbers are unquoted: 更改它,以便数字不被引用:

var data = [
{ label: 'Wins',  data: <?php echo $win ?>, color:'green'},
{ label: 'Draws',  data: <?php echo $draw ?>, color:'orange'},
{ label: 'Losses',  data: <?php echo $loss ?>, color:'red'}
];

Evidently your PHP variables are outside of the scope, that is because echo returns ''. 显然,您的PHP变量不在范围内,这是因为echo返回''。

There is a good topic describing " How to define a variable in JavaScript with PHP echo function? " 有一个很好的主题,描述了“ 如何使用PHP echo函数在JavaScript中定义变量?

PS: For testing purpose if it is possible you may try to embed javascript snippet into PHP-script where variables are declared. PS:出于测试目的,如果可能的话,您可以尝试将javascript代码段嵌入到声明了变量的PHP脚本中。

You are trying to run php in a javascript file. 您正在尝试在JavaScript文件中运行php。 The browser can not process server code. 浏览器无法处理服务器代码。 php runs on server only. php仅在服务器上运行。

What you can do is pass the php variable to a javascript variable as follows: Within page ( prior to script tag for external script): 您可以做的是将php变量传递给javascript变量,如下所示:在页面内(外部脚本的脚本标签之前):

$record = "283-161-151";
$rec = explode("-", $record);
echo '<script>var plotData='.json_encode($rec).';</script>';
/* displays var plotData=[283,161,151]; */

Now using the javascript variable that already exists JS will be: 现在,使用已经存在的JS的javascript变量将是:

var data = [
{ label: 'Wins',  data: plotData[0], color:'green'},
{ label: 'Draws',  data: plotData[1], color:'orange'},
{ label: 'Losses',  data: plotData[2], color:'red'}
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM