这对我来说很难,我想不出来。我有2个.php文件(一个与iframe另一个与注释。所以我们把它们分别命名为iframe.php和comment.php)。在iframe.php文件中,我从数据库中获得随机url,并将其作为src的<iframe>
:
$sql = mysql_query("SELECT url FROM address_book ORDER BY RAND() LIMIT 1")
在comments.php文件中,我从相同的数据库表中获得随机url:
$sql = mysql_query("SELECT comment FROM address_book ORDER BY RAND() LIMIT 1")
然后在index.php页面我有按钮:
<button id="nextfLuky" onClick="viewNext();return false;">Randomize</button>
这个按钮调用以下javascript函数,从iframe.php文件插入到<div id="iframe"></div>
(位于index.php页面)。并且comment.php文件中的注释也插入<div id="comment"></div>
(也位于index.php文件中)
这是<button>
调用的javascript
function viewNext()
{
$("#iframe").load("iframe.php");
$("#coment").load("coment.php");
}
问题是,我需要得到的url和评论是相互关联的数据库(我可以做到,如果我使用一个。php文件,但我需要使用两个)。当我在两个不同的文件中随机获得它们时,它们在index.php文件中显示时彼此不相关。
那么,如果我在两个不同的文件中随机获取url和注释,我如何获得相互关联的url和注释呢?
新增:Database Table Structure
______________________________________
id |int(11) |Auto Increment
______________________________________
url |varchar(255)
______________________________________
comment |text(1000)
______________________________________
如果您喜欢JQuery,我将用JQuery代替XmlHttp解决方案。查看一下信息是如何传递的,我想根据你的需要进行调整并不难。
您只需要创建一个getUrl.php文件来获取url(我已经创建了一个用于测试目的)。iframe.php和comment.php将保持完整, *只是确保它们从$_GET['url']中获得随机url。*
我再次注意到,这是一个根据你目前的设计的解决方案,而不是一般的或最好的。
要测试它,让我们说这是index.php (你只需要viewNext()从这里其他代码用于测试):
<html>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
function viewNext()
{
$.get("getUrl.php",null,
function(responseText)
{ $("#iframe").attr('src', 'iframe.php?url='+responseText);
$("#comment").load('comment.php','url='+responseText);
}
);//load ends
}//viewNext ends
</script>
</head>
<body onload="viewNext()">
<input type=submit value=next id="mySubmit" onclick="viewNext()"><br>
<IFRAME id="iframe" WIDTH=450 HEIGHT=100>
Ybrowser doesn't show IFRAME.
</IFRAME>
<div id="comment"></div>
</body>
</html>
下面是iframe.php(带有测试代码):
<?PHP
echo 'Hello from <font color=red>iframe.php</font> I got the url, here it is:<br>
<font color=blue>',$_GET['url'],'</font>';
?>
下面是comment.php(带有测试代码):
<?PHP
echo 'Hello from comment.php I got the url, here it is:<br>
<font color=blue>',$_GET['url'],'</font>';
?
最后是getUrl.php(复制粘贴-或者包含-你的代码在这里得到随机url,只是回叫它!):
<?PHP
echo 'http://www.url',rand(1,9999),'.com';
?>