2013-07-27 71 views
3

我試圖從父頁面創建一個彈出頁面,該頁面將返回一個值到父頁面並最終關閉。使用php和javascript從彈出窗口返回值

我迄今所做的:

main.php:

<tr> 
<th>Project Name</th> 
<td><input type="text" name="project_name" id="pid" disabled="disabled" /> 
    <input type="button" name="choice" onClick="selectValue('id')" value="?"></td> 
</tr> 

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

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

</script> 
</head> 

search_project.php:

<head> 
<script> 
function closeWin(){ 
    myWindow.close(); 
} 
</script> 

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


<?php 
$sql = mysql_query("SELECT project_id from prjct where project_id like 'default'"); 
$num = mysql_num_rows($sql); 
<tr> 
<td><input type="button" value="Select" onClick="sendValue('<?php echo $sql['project_id']; ?>')" /></td> 
<td align="center"><? echo $sql['project_id']; ?></td> 
</tr> 

所以,它應該關閉彈出(search_project.php )並在main.php的輸入字段中返回project_id的值。但是,當我點擊選擇按鈕時沒有任何事情發生。彈出窗口不會關閉,並且不會返回該值。似乎sendValue(值)不起作用。

需要幫助。

+0

你傳遞兩個參數爲'window.opener.updateValue(parentId的,值);'但它只是設置接受一個。 – DevZer0

+0

@ DevZer0,我錯誤地寫了'function selectValue(pid)',其實際上是: 'function updateValue(pid,value)' – aki2all

+0

確保你通過'http://'而不是'file://' – DevZer0

回答

1

你真的想在這裏使用encodeURIComponent(pid)嗎?

嘗試沒有encodeURIComponent方法:

window.open('search_project.php?id=pid','popuppage', 
     'width=400,toolbar=1,resizable=1,scrollbars=yes,height=400,top=100,left=100'); 
+0

感謝的人..你讓我的一天:) – aki2all

+0

你歡迎... –