我知道这已经被问过很多次了,但是在阅读了有关该主题的所有其他帖子后,我仍然遇到了问题...在我的 php 代码和它所在的 javascript 之间的某个地方,我的数组正在运行。
在附加的代码中,我有一个用于调试 php.ini 的回显。当我从 javascript 中剪下 php 部分并在打开 echo 的情况下单独运行它时,它显示它正在正确构建我的 json_encoded 数组。
在 php 结束后的 javascript 中,我将 php 分配给 javascript 变量,因此我可以将其用于进一步处理(绘制图形)。放入display语句,将获取数组的php调用结果内容显示到javascript中,显示数组为空。
如果我剪切并粘贴 php echo 的输出并将此文字分配给 javascript chartData 数组,那么一切正常。为什么javascript没有获取php数组内容?
这是代码片段:
<script>
...some java script stuff;
<?php
// Define the mySQL db connection
$db = new PDO('mysql:host=localhost;dbname=remets;charset=UTF-8', 'remets', 'remets', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
// Define SQL query to fetch data from mySQL
$stmt = $db->query("SELECT WeekNumber,XAxisCategory,YAxisValue FROM Metric WHERE ReportID = 'Q3' ORDER BY WeekNumber,XAxisCategory ASC");
// declarations
$amData = array();
$amArray = array();
$ctrinner = 0;
$ctrouter = -1;
$prevweek = "9999";
// Fetch data from mySQL and put it in an array in the format we need
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($prevweek !== $row['WeekNumber']) {
$ctrouter++;
$ctrinner = 0;
$amData[$ctrouter]["week"] = "".$row['WeekNumber']; // Prepending (or appending) the empty string makes the json encoding think the week number is a string, which is MUST have for AmCharts
}
$ctrinner++;
$amData[$ctrouter][$row['XAxisCategory']] = $row['YAxisValue'];
$prevweek = $row['WeekNumber'];
}
// Using json_encode puts the data into an array format that we can use in a javascript
$amJSONArray = json_encode($amData);
// Echo is for debugging only.
// echo $amJSONArray;
?>
var chartData = <?php echo $amJSONArray;
?>;
...more javascript stuff;
</script>
@Mahdi: print_r 的输出是: Array ( [0] => Array ( [week] => 1301 [Accepted] => 30 [Failed] => 5 [Passed] => 20 [Planned] => 5 [跳过] => 5 [未知] => 26) [1] => 数组 ([周] => 1302 [接受] => 25 [失败] => 2 [通过] => 25 [计划] => 2 [跳过] => 3 [未知] => 20) [2] => 数组 ([周] => 1303 [接受] => 26 [失败] => 26 [通过] => 29 [计划] => 26 [跳过] => 26 [未知] => 10 ) )
@Mahdi:这是 php 之后的 jscript 代码(它被注释掉了,因为我尝试了很多不同的选项,这些选项在这个论坛的其他帖子和其他帖子中推荐 - 它们都不起作用。我可以运行 php 代码并且有效很好。如果我在我之前发布的 php 代码片段中复制 echo 的输出,然后简单地将其分配给 chartData(即:chartData =“”;我的图表制作得很好。问题不在于图表工具,而在于数组内容对 .js 文件中直接位于其下方的 javascript 不可见。感谢您到现在为止的时间。
//var chartData = "<?php print($amJSONArray); ?>"; // This just returns the literal in the speech marks
//var chartData = '<?php print($amJSONArray); ?>'; // This also returns the literal in the speech marks
//var chartData = "<?php echo($amJSONArray); ?>"; // This just returns the literal in the speech marks
//var chartData = '<?php echo($amJSONArray); ?>'; // This also returns the literal in the speech marks
//var chartData = <?php echo ($amJSONArray) ?>; // This returns empty
//var chartData = <?php echo $amJSONArray ?>; // This returns empty
//var chartData = (<?php echo $amJSONArray ?>); // This returns empty
//alert(chartData); // Returns empty - just showing the contents of the array if I do the json_encode within the php part
//alert(<?php echo $amJSONArray ?>); // Returns empty - just showing the contents of the array if I do the json_encode during the array fetch
更新:我认为我身边发生了一些根本性的错误。我使用了一个非常简单的示例,它应该在屏幕上写入“hello world”,但它什么也没返回。如果我用“警报”代替“写”,那么警报弹出窗口中仍然没有显示任何内容。有谁知道为什么这不起作用?代码是:
<?php
$testvar = "Hello World";
?>
<html>
<head>
<script type="text/javascript">
function hello()
{
// create JavaScript variable, fill it with Php variable
var testvar = "<? print $testvar; ?>";
// output to screen
document.write( testvar );
}
</script>
</head>
<!-- Call JavaScript function to display variable -->
<body onload="hello()" >
</body>
</html>