我正在尝试做的基础在mySQL中存储路径然后在服务器上存储图像。然后使用下面的PHP代码将它们加载到屏幕上并使用按钮移动它们。这在浏览器上很好。但是,这些操作实际上将在不接受PHP的cordova应用程序上完成。我一直在使用HTTP请求解决这个问题。将元素加载到div中。不幸的是,要从PHP文件加载HTML元素。但是JavaScript函数不起作用。甚至不是警报,尤其是用于将第一个和后续图像加载到数组中并显示它们的函数。
以下是我正在尝试使用HTTP请求显示的PHP端代码。
<!DOCTYPE html>
<html>
<?php
include("mysqlconnect.php");
$select_query = "SELECT `ImagesPath` FROM `offerstbl` ORDER by `ImagesId` DESC";
$sql = mysql_query($select_query) or die(mysql_error());
$data = array();
while($row = mysql_fetch_array($sql,MYSQL_BOTH)){
$data[] = $row['ImagesPath'];
}
$images = json_encode($data);
?>
<script>
var images = <?php echo $images; ?>
alert(images[1]);
var index = 0;
function buildImage() {
var img = document.createElement('img')
img.src = images[index];
document.getElementById('content').appendChild(img);
}
function changeImage(){
var img = document.getElementById('content').getElementsByTagName('img')[0]
index++;
index = index % images.length; // This is for if this is the last image then goto first image
img.src = images[index];
}
</script>
<body onload="buildImage();">
<div class="contents" id="content"></div>
<button onclick="changeImage()">NextImage</button>
</body>
</html>
这是我用来显示PHP代码的cordova中的HTML代码。但是我的JavaScript函数都不起作用。
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("phpFile").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("content").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http:server/content.php?");
xmlhttp.send();
}
</head>
<body onload="showUser()">
<div class="contents" id="phpFile">
</body>
</html>