Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Populate parent window from child

    A while ago I asked for help with a popup window for my php messaging system. The code below is what I was lead to.

    PHP Code:
    <script type="text/javascript">
        function 
    openWinNav() {
        
    win2 window.open("userselect.php""Navigation","width=1000,height=300,scrollbars=no,status=no");
    }
    </script> 
    PHP Code:
    <a href="javascript:openWinNav()">Select User</a
    PHP Code:
    <?php
    mysql_connect
    ("host""username","password") or die (mysql_error ());

    mysql_select_db("database") or die(mysql_error());
     
    // you can set a hidden field if you dont want people to edit the reply from here
    $strSQL "SELECT * FROM users"
    $rs mysql_query($strSQL);
    $Head .= '<script language="javascript">
    function SubmitPage(id,recip) {
        window.opener.document.getElementById(\'UserID\').value = id;
        window.opener.document.getElementById(\'UserName\').value = recip;
        this.close();
    }
    </script>'
    ;

    while(
    $row mysql_fetch_array($rs)) {

    echo 
    '<input type="button" name="Select" value="'.$row['username'].'" onClick="SubmitPage(\''.$row['id'].'\',\''.$row['username'].'\')">';
            }  
            
    ?>
    Unfortunately I was never able to get it to work and as it wasn't vital I left it alone. I'm now expanding the site and would like to add the function. If someone can help with this I would be very grateful.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    What you have will give you a popup window that has a button for every one in your users table. The buttons will have the 'username' as it's value (label).
    This does work for you to this point RIGHT?

    When a button is clicked (choosing a user) what is suppose to happen then?
    I ask because the javascript that is in the popup does nothing.
    What it wants to do is put the user name and user id into (maybe a div(s)) on the page that opened the popup and which you do not show in the second box.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Played with this a little.
    Try this:
    The HTML:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body>
    <a href="javascript:openWinNav()">Select User</a><br />
    
    User ID..<div id="UserID"></div><br />
    User Name..<div id="UserName"></div><br />
    
    <script type="text/javascript">
        function openWinNav() {
        win2 = window.open("userselect.php", "Navigation","width=1000,height=300,scrollbars=no,status=no");
    }
    </script>
    </body>
    </html>
    The PHP:
    PHP Code:
     <?php
    mysql_connect
    ("host""username","password") or die (mysql_error ());

    mysql_select_db("database") or die(mysql_error());

    // you can set a hidden field if you dont want people to edit the reply from here
    $strSQL "SELECT * FROM users";
    $rs mysql_query($strSQL);

    $Head '<script language="javascript">
    function SubmitPage(id,recip) {
        window.opener.document.getElementById(\'UserID\').innerHTML = id;
        window.opener.document.getElementById(\'UserName\').innerHTML = recip;
        this.close();
    }
    </script>'
    ;

    while(
    $row mysql_fetch_array($rs)) {
        echo 
    '<input type="button" name="Select" value="'.$row['username'].'" onClick="SubmitPage(\''.$row['id'].'\',\''.$row['username'].'\')">';
        echo 
    '<br />'.$Head;
    }
    ?>
    Last edited by sunfighter; 10-09-2013 at 03:03 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #4
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    You're right it gets right up to the list of users then nothing. I tested the one you posted and it works. I've not been able to integrate it in to mine though, I'm wanting it to populate a text box on my send message form, the id is 'recip'.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Put the
    Code:
    echo '<br />'.$Head;
    line after while loop. Much better to only have one JS script in the popup.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #6
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Not sure what you mean. I assume I need to change something in this

    PHP Code:
    $Head '<script language="javascript">
    function SubmitPage(id,recip) {
        window.opener.document.getElementById(\'UserID\').innerHTML = id;
        window.opener.document.getElementById(\'UserName\').innerHTML = recip;
        this.close();
    }
    </script>'

    to match my html, this

    PHP Code:
    <label for="recip">Recipient<span class="small">(Username): </span></label><input type="text" value="<?php echo htmlentities($orecipENT_QUOTES'UTF-8'); ?>" id="recip" name="recip" /><a href="javascript:openWinNav()">Select User</a><br /><br />
    I also assume I don't need 'UserID' as I only want to populate one textbox.

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    The php:
    PHP Code:
    <?php
    mysql_connect
    ("host""username","password") or die (mysql_error ());

    mysql_select_db("database") or die(mysql_error());

    // you can set a hidden field if you dont want people to edit the reply from here
    $strSQL "SELECT * FROM users";
    $rs mysql_query($strSQL);

    $Head '<script language="javascript">
    function SubmitPage(id,recip) {
        window.opener.document.getElementById(\'UserID\').innerHTML = id;
        window.opener.document.getElementById(\'UserName\').innerHTML = recip;
        this.close();
    }
    </script>'
    ;

    while(
    $row mysql_fetch_array($rs)) {
        echo 
    '<input type="button" name="Select" value="'.$row['username'].'" onClick="SubmitPage(\''.$row['id'].'\',\''.$row['username'].'\')">';
    }
    echo 
    '<br />'.$Head;
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #8
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Does the fact I'm trying to populate a textbox make a difference?

  • #9
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Try this:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body>
    <a href="javascript:openWinNav()">Select User</a><br />
    
    <!--User ID..<div id="UserID"></div><br />
    User Name..<div id="UserName"></div><br />-->
    <input type="text" id="UserName" value="" />
    
    <script type="text/javascript">
        function openWinNav() {
        win2 = window.open("userselect.php", "Navigation","width=1000,height=300,scrollbars=no,status=no");
    }
    </script>
    </body>
    </html>
    PHP Code:
     <?php
    mysql_connect
    ("host""username","password") or die (mysql_error ());

    mysql_select_db("database") or die(mysql_error());

    // you can set a hidden field if you dont want people to edit the reply from here
    $strSQL "SELECT * FROM users";
    $rs mysql_query($strSQL);

    $Head '<script language="javascript">
    function SubmitPage(recip) {
        window.opener.document.getElementById(\'UserName\').value = recip;
        this.close();
    }
    </script>'
    ;

    while(
    $row mysql_fetch_array($rs)) {
        echo 
    '<input type="button" name="Select" value="'.$row['username'].'" onClick="SubmitPage(\''.$row['username'].'\')">';
    }
    echo 
    '<br />'.$Head;
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #10
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    That no allows the popup window to close but doesn't populate the text box.

  • #11
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Works for me. did you change something?

    It almost certainly means this line
    Code:
    window.opener.document.getElementById(\'UserName\').value = recip;
    Could not find an ID="UserName" in the HTML. Everything else would generate an error.
    Check this line in the HTML
    Code:
    <input type="text" id="UserName" value="" />
    Last edited by sunfighter; 10-09-2013 at 06:45 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #12
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Nope, didn't change anything.

    Edit: I re-copied everything to be sure I hadn't missed anything and it is working now.

    Thank you.
    Last edited by Foster; 10-09-2013 at 06:51 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •