3

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.

4
  • your passing two parameters to window.opener.updateValue(parentId, value); but its only setup to accept one. Commented Jul 27, 2013 at 11:22
  • @DevZer0, I wrongly wrote function selectValue(pid), its actually: function updateValue(pid, value) Commented Jul 27, 2013 at 11:43
  • make sure you access the page via http:// not file:// Commented Jul 27, 2013 at 11:44
  • I am working on a server. So, I am using http:// Commented Jul 27, 2013 at 11:46

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.