【发布时间】:2010-09-26 05:34:54
【问题描述】:
我想显示一个图像作为对使用 php 的 ajax 调用的响应。有人有完整的代码吗?请帮帮我。。 提前谢谢..
【问题讨论】:
我想显示一个图像作为对使用 php 的 ajax 调用的响应。有人有完整的代码吗?请帮帮我。。 提前谢谢..
【问题讨论】:
好吧,我没有完整的代码,如果有,我不会给你。
您需要首先使用PHP's various image generation functions 创建图像。要使其动态化,您可以发送它various parameters which are encoded in the url,并且可以使用 php 中的 $_GET 超全局来获取。
然后,您可以将现有图像占位符元素的 src 设置为动态图像的位置,然后瞧!即时动态图像。
【讨论】:
您应该使用jQuery + JSON 组合。您可以使用json_encode将php数组转换为JSON格式。
index.php:
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<a href='car.php' class='ajax'>Car Image</a>
<a href='bike.php' class='ajax'>Bike Image</a>
<div id="title">Title comes here</div>
<div id="image">Image comes here</div>
car.php:
<?php
$jsonArray['title'] = "Car";
$jsonArray['image'] = "<img src='images/car.jpeg'>";
echo json_encode($jsonArray);
?>
bike.php:
<?php
$jsonArray['title'] = "Bike";
$jsonArray['image'] = "<img src='images/bike.jpeg'>";
echo json_encode($jsonArray);
?>
ajax.js:
jQuery(document).ready(function(){
jQuery('.ajax').click(function(event) {
event.preventDefault();
jQuery.getJSON(this.href, function(snippets) {
for(var id in snippets) {
jQuery('#' + id).html(snippets[id]);
}
});
});
});
【讨论】: