这就是我正在做的事情。
我正在建立一个网站,来自 twitter 的用户从他们的 tweetbot 客户端将图像上传到我的网站。我的应用程序从 tweetbot 检索照片并将其上传到我的 racskpace 云服务器。我已经成功地完成了这一切。我现在正在设计一个如下所示的用户界面。还有一个网络上传器。
网络上传器工作正常。我想要的是,请注意下面的三张图片。我希望它改变生活。例如,如果用户通过他们的 tweetbot 客户端上传照片,照片应该出现在这里。上传过程全部通过文件 /api/index.php 完成。所以无论我需要放什么代码,这样每当用户上传时,文件 /api/index.php 就会被执行,我的 UI 应该会实时反映。
因此,我在 AJAX 中进行了一些研究来做到这一点。我在 /api/index.php 中包含了以下函数
<script type="text/javascript">
function showPhoto(str) {
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) {
$shortenedurl = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getphoto.php?q=" + str, true);
xmlhttp.send();
}
</script>
我还通过在 /api/index.php 文件末尾添加 showPhoto($shortenedurl) 来执行该功能
getPhoto.php 看起来像这样:
<?php
$q=$_GET["q"];
$con = mysql_connect("","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
$result = mysql_query("SELECT * FROM tblphoto WHERE shorturl = '$q'");
$row = mysql_fetch_array( $result );
$tweet = $row['tweet'];
$sn = $row['user'];
$thumb = $row['thumb'];
mysql_close($con);
?>
<div class="one-third column feature">
<h2>
<?php echo $sn; ?>
</h2>
<img src=<?php echo $thumb; ?>/>
<p>
<?php echo $tweet; ?>
</p>
</div>
<div class="one-third column feature">
<h2>Two</h2>
<img src="http://lorempixum.com/400/100/nature/2" />
<p>Lorem ipsum dolor sit amet...</p>
</div>
<div class="one-third column feature">
<h2>Three</h2>
<img src="http://lorempixum.com/400/100/nature/3" />
<p>Lorem ipsum dolor sit amet...</p>
</div>
然后我在我的 UI 的 index.html 中包含 getPhoto.php。
我还希望能够遍历数据库并一次只显示三个图像。因此,如果用户上传照片,最左边的照片将更改为最新的。前一个最左边占据第二个位置,前一个中间占据第三个位置。最右边的照片被丢弃。
现在,什么都没有显示,如屏幕截图所示。是在正确的方向还是我做错了?