English 中文(简体)
javascript + PHP - 从弹出向另一个页面发送值
原标题:javascript + PHP - send value from popup to another page

我有一个工作脚本 从弹出页面传到父页的值。

Process Flow is: ParentPage>ChildPage>ParentPage

I have had to change the process flow of my little application to have a category select option on the child page. the process flow is now: ParentPage>ChildPage1>ChildPage2>ParentPage

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

我希望我已经准确地解释了我的问题。 我怎样才能将选择的值 回到原父页。 如果父母的页面被命名为父/ php, 我能否在代码中硬编码父. php 的代号 :

window.opener.updateValue(parentId, value);

任何援助都是感激不尽的。

我的工作守则和拟议守则列示如下:

<强度>/强度 > < wORKing CODE

< 加强 > 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> 

< 强/强 > CODE < /强 >

< 加强 > 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>

我知道弹出页面在目前情况下并不是理想的选择, 但这是我的应用程序所在的位置 。 请告知如何修改孩子2的代码, 以指向父母的.php 页面 。 我的想法是修改下面的代码 。

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

此代码的代码( 硬码父代号 - 父代号是什么 。 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> 

Many Thanks, Ryan

最佳回答

好吧,这是我的建筑建议摘要:

主要 :

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

儿童 1.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)

儿童 2.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)

在这里,我们走了: -)

问题回答

你只要粗暴地回答问题, 你可以这样做:

window.opener.updateValue(parentId, value);

I personnally use this system: - main window MAIN - open a popup to pick up a category POPUP - selects a value from popup, setup the category field in MAIN this way:

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

希望它能帮上忙!

ps: 我没有尝试 window.opener.opener , 但我不明白为什么它不起作用...

edit: child2 is posted this:

$category=$_POST[category];

You could simply open child2 like this: in child1:

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

and retrieve the value for category from child2 in javascript using window.category instead of getting it from php $category. Note that in child2.php you don t use the $category variable.

希望有帮助

而不是通过表格。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签