这是arrivalPlay.php。如果用户单击来自arrivalRead.php的数据并使url变成arrivalPlay.php?id = 1(2,3,4,5等等),则加载此页面。使用jquery从动态php页面检索数据到javascript
<?php
$con = mysqli_connect("localhost","admin","admin","flight_status");
$id = $_GET['id'];
$getrow = mysqli_query($con, "SELECT * FROM arrival WHERE id='$id'");
$row = mysqli_fetch_array($getrow);
mysqli_close($con);
$order = array(1,2,3,4);
foreach ($order as $o) {
$res[$o][f] = $row[$o];
}
json_encode($res);
?>
这是getData.js文件。该文件文件接收res并将被传递给'mp'。
<script>
function aha() {
$.ajax({
url:'arrivalPlay.php',
data:{id:3},
dataType:'json',
type:'GET',
success:function(data){
document.write(data[1].f);
document.write(data[2].f);
document.write(data[3].f);
document.write(data[4].f);
}
});
}
</script>
页arrivalPlay.php只有数据如果URL成为arrivalPlay.php?ID = X。有没有办法从'动态'的PHP检索到JavaScript页面的数据?如果您认为这很奇怪,请随时改变我的方法。谢谢...
对不起,对于迟到的答案。回显确实打印了'$ res'的内容。上面的代码不起作用,我的JavaScript不加载任何东西。我想知道为什么。 – epndkempot 2013-05-03 00:32:32
@epndkempot在使用之前,请尝试上面的操作,并检查是否已添加'jquery'的任何'版本'。 – 2013-05-03 04:43:30
感谢您的建议。我使用json并删除不必要的元素。在接收端,现在可以打印“null - 1 null - 2 null - 3 null - 4”。如果我更改为'$ .getJSON('arrivalPlay.php?id = 1',它将检索数据。 – epndkempot 2013-05-03 06:45:21