2012-05-27 43 views
2

我有一个工作脚本,将一个值从一个弹出页面传递到父页面。JavaScript + PHP的 - 从弹出窗口发送值到另一页

工艺流程是: ParentPage> ChildPage> ParentPage

我不得不改变我的小应用程序的流程有一个“类别中选择”子页面上的选项。现在的处理流程为: ParentPage> ChildPage1> ChildPage2> ParentPage

我的应用程序现在要求用户从弹出窗口中选择一个值。第一个弹出窗口将提供类别选择。一旦选择一个类别,类别产品将显示在下一页。用户然后从选项中选择一个产品,并将该值传递给原始父页面。

我希望我已经准确地解释了我的问题。我如何将选定的值传递回原始父页面。如果我的父母页面命名为parent.php,可我硬代码页parent.php的ID为我的代码:

window.opener.updateValue(parentId, value); 

任何援助总是赞赏。

我工作的代码,并提出代码如下所示:

工作代码

parent.php

<html> 
<head> 
<title>test</title> 

<script type="text/javascript"> 
function selectValue(id) 
{ 
    // open popup window and pass field id 
    window.open('child.php?id=' + encodeURIComponent(id),'popuppage', 
     'width=400,toolbar=1,resizable=1,scrollbars=yes,height=400,top=100,left=100'); 
} 

function updateValue(id, value) 
{ 
    // this gets called from the popup window and updates the field with a new value 
    document.getElementById(id).value = value; 
} 

</script> 
</head> 

<body> 

<table> 

<tr> 
<th>PACK CODE</th> 
</tr> 

<tr> 
    <td> 
     <input size=10 type=number id=sku1 name=sku1 ><img src=q.png name="choice" onClick="selectValue('sku1')" value="?"> 
    </td> 
</tr> 
<tr> 
    <td> 
     <input size=10 type=number id=sku2 name=sku2 ><img src=q.png name="choice" onClick="selectValue('sku2')" value="?"> 
    </td> 
</tr> 
</table> 


</body> 
</html> 

child.php

<script type="text/javascript"> 
function sendValue(value) 
{ 
    var parentId = <?php echo json_encode($_GET['id']); ?>; 
    window.opener.updateValue(parentId, value); 
    window.close(); 
} 
</script> 
<table> 
    <tr> 
     <th>Pack Code</th>      
    </tr> 
    <tr> 
     <td>1111</td> 
     <td><input type=button value="Select" onClick="sendValue('1111')" /></td> 
    </tr> 
    <tr> 
     <td>2222</td> 
     <td><input type=button value="Select" onClick="sendValue('2222')" /></td> 
    </tr> 
    <tr> 
     <td>3333</td> 
     <td><input type=button value="Select" onClick="sendValue('3333')" /></td> 
    </tr> 
</table> 

推荐码

parent.php

<html> 
<head> 
<title>test</title> 

<script type="text/javascript"> 
function selectValue(id) 
{ 
    // open popup window and pass field id 
    window.open('child1.php?id=' + encodeURIComponent(id),'popuppage', 
     'width=400,toolbar=1,resizable=1,scrollbars=yes,height=400,top=100,left=100'); 
} 

function updateValue(id, value) 
{ 
    // this gets called from the popup window and updates the field with a new value 
    document.getElementById(id).value = value; 
} 

</script> 
</head> 

<body> 

<table> 

<tr> 
<th>PACK CODE</th> 
</tr> 

<tr> 
    <td> 
     <input size=10 type=number id=sku1 name=sku1 ><img src=q.png name="choice" onClick="selectValue('sku1')" value="?"> 
    </td> 
</tr> 
<tr> 
    <td> 
     <input size=10 type=number id=sku2 name=sku2 ><img src=q.png name="choice" onClick="selectValue('sku2')" value="?"> 
    </td> 
</tr> 

</table> 

</body> 
</html> 

child1.php

<script type="text/javascript"> 
function sendValue(value) 
{ 
    var parentId = <?php echo json_encode($_GET['id']); ?>; 
    window.opener.updateValue(parentId, value); 
    window.close(); 
} 
</script> 
<form name="category" method="POST" action=child2.php> 
<table> 
<tr> 
<td>Category</td> 
</tr> 
<tr> 
<td> 
<select name="category" id="category"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
    <option value="4">4</option> 
</select> 
</td> 
</tr> 
<tr> 
<td> 
<input type=submit value=next> 
</td> 
</tr> 
</table> 

Child2.php

<script type="text/javascript"> 
function sendValue(value) 
{ 
    var parentId = <?php echo json_encode($_GET['id']); ?>; 
    window.opener.updateValue(parentId, value); 
    window.close(); 
} 
</script> 


<? 

$category=$_POST[category]; 

?> 
<form name="selectform"> 
<table> 
    <tr> 
    <tr> 
     <th>Pack Codes for Category <? echo $category; ?></th>      
    </tr> 
    <tr> 
     <td>1111</td> 
     <td><input type=button value="Select" onClick="sendValue('1111')" /></td> 
    </tr> 
    <tr> 
     <td>2222</td> 
     <td><input type=button value="Select" onClick="sendValue('2222')" /></td> 
    </tr> 
    <tr> 
     <td>3333</td> 
     <td><input type=button value="Select" onClick="sendValue('3333')" /></td> 
    </tr> 
    </table> 

我知道在这种情况下弹出页面不是期望的选项,但这是我的应用程序所在的位置。请告诉我如何修改child 2上的代码以指向parent.php页面。我的想法是改变下面的代码

<script type="text/javascript"> 
function sendValue(value) 
{ 
    var parentId = <?php echo json_encode($_GET['id']); ?>; 
    window.opener.updateValue(parentId, value); 
    window.close(); 
} 
</script> 

此代码(硬编码父母的ID在 - 这将是父母。PHP)

<script type="text/javascript"> 
function sendValue(value) 
{ 
    var parentId = <?php echo json_encode($_GET['id']); ?>; 
    window.opener.updateValue('parent.php', value); 
    window.close(); 
} 
</script> 

非常感谢, 瑞安

回答

2

好吧,这里是我的架构命题内容摘要:

主:

var oChild = window.popup('child1.php'); 
oChild.openerr = window; 

child1.php:

var oWin = window.popup('child2.php'); 
oWin.category = document.getElementById('category').value; 
oWin.openerr = window; 
//here use window.openerr to access the parent (main) 

child2.php:

var g_oXhr = new xmlHttpRequest(); 
    g_oXhr.onreadystatechange = function() { 
     if (g_oXhr.readyState == 4 && (g_oXhr.status == 200)) { 
      //use g_oXhr.responseText or g_oXhr.responseXML 
     } 
    } 

    g_oXhr.open("POST", "mysql_query_elements_category.php", true); 
    g_oXhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8"); 
    g_oXhr.send('0=' + encodeURIComponent(window.category)); 

    //here use window.openerr to access the parent (child1) 

这里我们去:-)

+0

是否有可能从子窗口到父窗口返回如果是的话可以怎么做我这样做 –

+0

编辑示例添加回父母的可能性 – Sebas

1

只是回答残酷的问题,是的,你可以这样做:

window.opener.updateValue(parentId, value); 

我personnally使用本系统: - 主窗口主 - 打开一个弹出框来选取一个类别POPUP - 从弹出框中选择一个值,以这种方式设置MAIN中的分类字段:

var oElem = window.opener.document.getElementById("myElementId"); 
oElem.value = '...'; 

希望它有帮助!

PS:我没有尝试window.opener.opener但我不明白为什么它不会工作...

编辑: 的child2张贴这样的:

$category=$_POST[category]; 

你可以简单地开的child2是这样的:在 child1:

wherever.onclick = function(e) { 
var ochild2 = window.open('child2.php'); 
ochild2.category = document.getElementById('category').value; 
} 

和检索利用window.category在JavaScript中从类的child2价值而不是从php $category中得到它。 请注意,在child2.php中,您不使用$ category变量。

希望它有帮助

而不是通过一种形式。

+0

嗨,感谢您的及时响应。我对此很新,所以还在加快速度。 parentId应该是什么? parent.php?或者我如何将你的代码建议代码整合到我现有的页面中。谢谢,R – Smudger

+1

我看到了,问题是,child2不是弹出式窗口...我认为阅读你,但检查你的代码我知道你通过表单验证目标去那个页面。你在这种情况下失去了window.opener链... – Sebas

+0

100%正确。所以现在需要指定父页面而不是依赖于window.opener.updateValue(parentId,value);这可以做到吗?谢谢,R – Smudger