我有一个工作脚本,将一个值从一个弹出页面传递到父页面。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>
非常感谢, 瑞安
是否有可能从子窗口到父窗口返回如果是的话可以怎么做我这样做 –
编辑示例添加回父母的可能性 – Sebas