Search code examples
phpjavascriptmysqlwindowparent

PHP determining what to insert into parent window textarea from child window using radio buttons in child window


Hi everyone im new to php and would like to know how if it is possible determine what to insert into parent window textarea from child window using radio buttons in child window. How should I go about doing it? I would much appreciate it if anyone could provide some help on this. Below is the code. Thanks.

Parent Window:

<head>
<script type='text/javascript'>
window.onload = function()
{
  document.getElementById('b1').onclick = openChild;
}
function openChild()
{
  this.disabled = false;
  xWinOpen('retrievemath.php');
}
var xChildWindow = null;
function xWinOpen(sUrl)
{
  // Modify 'features' to suit your needs:
  var features = "left=100,top=100,width=400,height=400,location=0,menubar=0," +
    "resizable=1,scrollbars=1,status=0,toolbar=0";
  if (xChildWindow && !xChildWindow.closed) {xChildWindow.location.href  = sUrl;}
  else {xChildWindow = window.open(sUrl, "myWinName", features);}
  xChildWindow.focus();
  return false;
}
</script>
<script src="ckeditor/ckeditor.js"></script>

</head>
<body>
    <textarea class="ckeditor" cols="80" id="editor1" name="editor1" rows="10" ></textarea>
<form>
<input type="button" id ="b1" onClick="myPopup2()" value="POP2!">
</form>
<p onClick="myPopup2()">CLICK ME TOO!</p>
</body>

Child Window:

<?php
include ('database_connection.php');
  $dbConn = mysql_connect('localhost', 'root', '')
             or trigger_error(mysql_error(), E_USER_ERROR);
  // select the database for use
  mysql_select_db('test', $dbConn);
$query_retrieve_expression = "SELECT * FROM math";
 $queryResource = mysql_query($query_retrieve_expression, $dbConn) or die(mysql_error());
?>

<head>

<script type='text/javascript'>
window.onload = function()
{
  document.getElementById('f1').onsubmit = sendToParent;
}
function sendToParent()
{
  if (window.opener) {
    var cta = document.getElementById('<?php echo $row['mathID']?>');
    var pta = window.opener.document.getElementById('editor1');
    pta.value = cta.value;
    window.opener.focus();
  }
  return false;
}
</script>
</head>
<body>
        <form action="retrievemath.php" method="post" id="f1">
<table class="hovertable">
<tr>
    <th>Insert ?</th><th>Expression Name</th><th>Math Expression</th>
</tr>

    <?php
               while($row = mysql_fetch_assoc($queryResource))
               {     
    ?>

                    <tr> 
                    <td><input type="radio" name="insert" id="<?php echo $row['mathID']?>" value="<?php echo $row['expressionname']?>" /> </td> 
                    <td><?php echo $row['expressionname']; ?></td>
                    <td><?php echo $row['mathexpression']; ?></td> 
                    </tr>

    <?php
               }
    ?>


</table>



    <div class="submit">
      <input type="submit" value="Insert" />
    </div>
        </form>
</body>

Solution

  • You can get value for textarea and passed in url like this

    Latest function

    function sendToParent()
    {
        var cta='';
    
        var radio=document.getElementsByName("insert");
    
        for(var i=0;i<radio.length;i++)
        {
            cta+=radio[i].value;
        }
    
        var pta = var pta = window.opener.document.getElementById("editor1");
        pta.value = cta;
        self.close();
    }
    

    You can get value of textarea in child window like this

    echo $_GET['data'];
    

    Child window code

    <head>
    <script type='text/javascript'>
    
     function sendToParent()
    {
        var cta='';
    
        var radio=document.getElementsByName("insert");
    
        for(var i=0;i<radio.length;i++)
        {
            cta+=radio[i].value;
        }
    
        var pta =var pta = window.opener.document.getElementById("editor1");
        pta.value = cta;
        self.close();
    }
    </script>
    </head>
    <body>
    <form action="retrievemath.php" method="post" id="f1">
    <table class="hovertable">
    <tr>
        <th>Insert ?</th><th>Expression Name</th><th>Math Expression</th>
    </tr>
    <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
    
    </tr>
    
        <?php
                   while($row = mysql_fetch_assoc($queryResource))
                   {     
        ?>
    
                        <tr> 
                        <td><input type="radio" name="insert" id="<?php echo $row['mathID']?>" value="<?php echo $row['expressionname']?>" /> </td> 
                        <td><?php echo $row['expressionname']; ?></td>
                        <td><?php echo $row['mathexpression']; ?></td> 
                        </tr>
    
        <?php
                   }
        ?>
    
    
    </table>
    
    
    
        <div class="submit">
          <input type="button" value="Insert" onclick="sendToParent();" />
        </div>
            </form>
    </body>