douchuo9476 2013-07-27 11:17
浏览 138
已采纳

使用php和javascript从弹出窗口返回一个值

I am trying to create a popup page from a parent page, which will return a value to the parent page and will close eventually.

What I have done so far:

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>

So, it is supposed to close the popup(search_project.php) and return the value of project_id in the input field of main.php. But, nothing is happening when I'm clicking on the select button. The popup doesn't close and the value is not returned. Seems sendValue(value) is not working.

Need help.

  • 写回答

1条回答 默认 最新

  • donglv7097 2013-07-27 13:23
    关注

    Do you really want encodeURIComponent(pid) here?

    Try without encodeURIComponent:

        window.open('search_project.php?id=pid','popuppage',
           'width=400,toolbar=1,resizable=1,scrollbars=yes,height=400,top=100,left=100');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看