2011-02-15 25 views
0

我正在研究一些将成为电子商务网站一部分的任务。你可能会看看它,并认为我应该用JS来做,但我们被告知几乎不使用任何JS,而是倾向于PHP,特别是在查询SQL表时。使用脚本在家长页面中刷新iframe

我在建立一些用户mouseOvers()目录中的任何图像时,一般的iFrame会根据具有当前焦点的图像刷新信息 - 它通过使用变量查询数据库被加载后。这是我使用JS提交刷新iFrame的表单的地方,并且由于PHP是解析服务器端,所以在刷新时可以将信息看似动态地加载到用户。

这一切似乎都在工作,除了目标iframe没有得到更新,而是页面被加载到浏览器(Firefox)的新选项卡或窗口中。注意:此代码是一个粗略的草稿!

<div class="contentpane"> 


<div class="image1" onMouseOver="jsFunction();" onMouseOut="jsFunction2();"> 
    <img src="http://t0.gstatic.com/images?q=tbn:ANd9GcStRUHQMqvpCvdCyEwkO9DFxy0IY9fg1CP3uePBktHRMz8QznlQ" width=100 height=100/> 

</div> 

<div class="image2"> 
</div> 

<div class="image3"> 
</div> 

<script> 
function jsFunction(){ 

    document.forms['testform'].elements['i1'].value = "mouseOver"; 
    document.forms["testform"].submit(); 
    // force refresh on Mouse Over 
    /*var f = document.getElementById('testiframe'); 
    f.contentWindow.location.reload(true);*/ 

} 

function jsFunction2(){ 

document.forms['testform'].elements['i1'].value = "mouseOut"; 
document.forms["testform"].submit(); 

} 

</script> 


<iframe src="iframetest.php" id='testiframe' width="200" height="200"> 
<!-- using this roundabout way with iframes so that all interaction with SQL is via PHP and not JS--> 
</iframe> 

<!-- hidden form that has a target of the iframe, submitted by the js functions--> 
<form id="testform" method="GET" target="testiframe" action="iframetest.php" > 
    <input type="hidden" id="i1" name="i1" value="hey"> <!-- retrieved in php file by $_GET--> 
</form> 

而对于iframetest.php代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link rel="stylesheet" href="style.css" /> 
<title>Home</title> 
</head> 

<body> 

Dynamic content, will be pulled from SQL via PHP <br/> <br/> 


<?php 
if(isset($_GET)){ 
echo $_GET["i1"]; 
} 
?> 

感谢您的帮助家伙!

回答

0

这个任务是从几英里远的尖叫阿贾克斯,但无论如何这里它: 内容显示在一个新的页面/标签是因为你没有指定的iframe名称,所以你的代码应该看起来像这个:

<iframe src="iframetest.php" name="testiframe" width="200" height="200"> 
<!-- using this roundabout way with iframes so that all interaction with SQL is via PHP and not JS--> 
</iframe> 

<!-- hidden form that has a target of the iframe, submitted by the js functions--> 
<form id="testform" method="GET" target="testiframe" action="iframetest.php" > 
    <input type="hidden" id="i1" name="i1" value="hey"> <!-- retrieved in php file by $_GET--> 
</form> 

ps请注意iframe标记中的变化从id='testiframe'变为name="testiframe"

+0

我认为name属性在XHTML中被宣告为不推荐,并且在不久的将来将不被支持? – ComethTheNerd 2011-02-15 09:43:01