【发布时间】:2014-12-25 21:15:27
【问题描述】:
这是我正在使用的代码:
<?php
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$) |(\.Gif$)"; //valid image extensions
$files = array();
$curimage=0;
if($handle = opendir($"http://newsxpressmedia.com/files/radar-simulation-files")) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
$files[] = $file;
$curimage++;
}
}
closedir($handle);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>change picture</title>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
#Timer_Countdown{
background:black;
color:yellow;
font-weight:bold;
text-align:center;
}
</style>
<script type = "text/javascript">
function displayNextImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.getElementById("img").src = images[x];
}
function displayPreviousImage() {
x = (x <= 0) ? images.length - 1 : x - 1;
document.getElementById("img").src = images[x];
}
var images = <?=json_encode($files)?>;
//var images = [];
var x = -1;
var swap_hours = 0;
var swap_minutes = 0;
var swap_seconds = 5;
var down_counter_hours;
var down_counter_minutes;
var down_counter_seconds;
function initTimer() {
down_counter_hours = swap_hours;
down_counter_minutes = swap_minutes;
down_counter_seconds = swap_seconds;
counter = setInterval(switcher, 1000);
}
function restartCounter() {
down_counter_hours = swap_hours;
down_counter_minutes = swap_minutes;
down_counter_seconds = swap_seconds;
}
function switcher() {
down_counter_seconds--;
if (down_counter_hours <= 0 && down_counter_minutes <= 0 && down_counter_seconds <= 0) {
swapColor();
restartCounter();
}
if (down_counter_seconds <= 0 && down_counter_minutes > 0) {
down_counter_seconds = 60;
down_counter_minutes--;
}
if (down_counter_minutes <= 0 && down_counter_hours > 0) {
down_counter_minutes = 60;
down_counter_hours--;
}
document.getElementById("Timer_Countdown").innerText = down_counter_hours+":"+down_counter_minutes+":"+down_counter_seconds;
}
function swapColor() {
displayNextImage();
}
</script>
<div id="div_hours" class="div_box"></div>
<div id="div_minutes" class="div_box"></div>
<div id="div_seconds" class="div_box"></div>
<div id="div_switcher" class="div_box"></div>
</head>
<body onload = "initTimer()">
<div id="Timer_Countdown"> </div>
<img id="img" src="http://newsxpressmedia.com/files/theme/radar000005.Gif">
<button onclick="displayPreviousImage(); restartCounter()">Previous</button>
<button onclick="displayNextImage(); restartCounter()">Next</button>
</body>
</html>
错误就行了:
var images = <?=json_encode($files)?>;
如果我将这一行改为这一行:
var images = [];
然后代码工作正常,但不使用 php 文件变量。
线路有问题:var images = <?=json_encode($files)?>;
我尝试将此行更改为:echo json_encode($images); 或var images = echo json_encode($files);,但同样的错误。
我正在使用 weebly 构建我的网站,而我的网站服务器位于 ipage.com 上
我该如何解决这个错误?
【问题讨论】:
-
所有代码都在 PHP 文件中吗?
-
在浏览器中点击“查看页面源代码”。那么问题应该很明显了
-
如果没有server-side configuration,您将无法从不是
.php的文件中执行PHP 代码。 -
我现在在 weebly.com 网站上看到了这个:我可以使用 PHP、MySQL 和其他服务器端语言吗? “您可以使用 HTML、CSS 和 Javascript(客户端语言)编辑您的网站;但是,Weebly 不支持使用 PHP 或 ASP 等服务器端语言构建您的网站。我们目前也不提供数据库访问。”这是否意味着我根本不能使用 php?
-
Phil 的视图页面源代码会清楚地说明 - 如果页面输出中存在您的所有 php 代码,那么就没有 php 解释器可以处理它。
标签: javascript php html