passing checkbox values from a popup to a parent

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
golodog
Forum Newbie
Posts: 3
Joined: Tue Apr 29, 2008 7:21 pm

passing checkbox values from a popup to a parent

Post by golodog »

Be gentle-- I am brand new at php and I'm sorry if I am asking a stupid question. I have been searching for a solution to my problem for 3 weeks. :banghead: In that time I have found several discussions on this topic but I still can't get it to work.

I have included code. On one page (test.php), I define a variable called "mynumber". There is a link which opens a popup (test1.php) and that number is displayed in the popup window. So, I am sending info successfully in one direction. On test1.php there are checkboxes and a text box. After filling in info there and clicking "Submit" I return to the original page (test.php) and display the text that was typed in the input box. So, I am successfully sending some info back as well.

However, I can not seem to get the checkbox info from test1.php back to test.php.

Thanks in advance for any help! :D


test.php:

Code: Select all

 
<html>
<head>
<title>test</title>
<script language="javascript" type="text/javascript">
 
<!--
function popitup(url) {
 
         newwindow=window.open(url,'name','height=400,width=450');
         if (window.focus) {newwindow.focus()}
         return false;
}
 
// -->
 
</script>
</head>
<body>
 
 
<form action="test.php" name="parentForm" method="POST">
<a href="test1.php?mynumber=50" onClick="popup = window.open('test1.php?mynumbe\
r=50', 'PopupPage', 'height=400,width=700,scrollbars=yes,resizable=yes'); retur\
n false" target="_blank">Click Here</a>
 
 
<input type="hidden" name="txt1" id="txt1"  />
<input type="hidden" name="boxes" id="boxes"  />
 
<!--this field will hold the child from value for textbox txt1-->
 
</form>
 
 
<?php
if($_POST){
print_r($_POST);
}
?>
 
 
 
</body>
</html>
 
 

test1.php:

Code: Select all

 
<html>
<head>
<script language="javascript"> function doSubmit(){
//set the parent hidden field value
window.opener.document.getElementById("txt1").value=
     document.getElementById("txt1").value;
//Submit parent form
window.opener.document.parentForm.submit();
//close this window
self.close(); }
 
</script>
</head>
<body>
 
<?php
$mynumber = $_GET['mynumber'];
echo $mynumber;
?>
 
<form action="test.php" method="post" name="childForm"
onsubmit="doSubmit()">
Box1:<input type="checkbox" name="boxes[]" value="Box1"><br />
Box2:<input type="checkbox" name="boxes[]" value="Box2"><br />
Box3:<input type="checkbox" name="boxes[]" value="Box3"><br />
<input type="text" name="txt1" id="txt1"> <input type="submit"
name="submit" value="Submit">
 
 
 
</form>
</body>
</html>
 
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: passing checkbox values from a popup to a parent

Post by VladSun »

It's not PHP related but Javascript related question.

In your test1.php you have a line:

Code: Select all

 //set the parent hidden field valuewindow.opener.document.getElementById("txt1").value= document.getElementById("txt1").value;
You have write similar lines for your checkboxes ...
There are 10 types of people in this world, those who understand binary and those who don't
golodog
Forum Newbie
Posts: 3
Joined: Tue Apr 29, 2008 7:21 pm

Re: passing checkbox values from a popup to a parent

Post by golodog »

:roll: Oh! I am an idiot. Thanks so much!
bartlett
Forum Newbie
Posts: 3
Joined: Thu May 15, 2008 11:01 pm

Re: passing checkbox values from a popup to a parent

Post by bartlett »

This is exactly what I was looking for. But, how do add that extra line properly. I tried this in test 1.php:

1) Add an id for the boxes array:

Code: Select all

 
<div id="boxes">
 
2) Add the line in the javascript:

Code: Select all

 
window.opener.document.getElementById("boxes").value=document.getElementById("boxes").value;
 
It always comes back in test.php as undefined.

Thanks.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: passing checkbox values from a popup to a parent

Post by VladSun »

<div> ???
You need a FORM element ...
There are 10 types of people in this world, those who understand binary and those who don't
bartlett
Forum Newbie
Posts: 3
Joined: Thu May 15, 2008 11:01 pm

Re: passing checkbox values from a popup to a parent

Post by bartlett »

I have a form element-- I am trying the identical code originally posted here. But, I couldn't seems to attach an id to the checkboxes any other way and getElementByName wasn't working.

??
bartlett
Forum Newbie
Posts: 3
Joined: Thu May 15, 2008 11:01 pm

Re: passing checkbox values from a popup to a parent

Post by bartlett »

Okay, I have the following two files, main.php and cb_page.php. Currently the doSubmit() function in cb_page.php does not work because it doesn't like

window.opener.document.getElementById("boxes").value=document.getElementById("boxes").value;

and I don't blame it because I have not set an id called boxes.

main.php:

Code: Select all

 
<html>
<body>
 
<form action="" name="parentForm" method="POST">
 <a href="cb_page.php"onClick="popup=
window.open('cb_page.php','PopupPage','height=400,width=700,scrollbars=yes,resizable=yes'); 
return false" target="_blank">Click Here</a>
 
<input type="hidden" name="boxes" id="boxes"  />
 
</form> 
 
<?php
if (isset($_POST['boxes'])) {
  foreach($_POST['boxes'] as $value) {
    echo $value;
    echo "<br>";
  }
}
else{echo "No selection.";}
?>
 
 
</body>
</html>
 
 

cb_page.php:

Code: Select all

 
<html>
<head>
 <script language="javascript"> function doSubmit(){
 //set the parent hidden field value
 window.opener.document.getElementById("boxes").value=
      document.getElementById("boxes").value;
 //Submit parent form
 window.opener.document.parentForm.submit();
//close this window
 self.close(); }
  
 </script>
</head>
<body>
 
 
<form name="form1" method="post" action="main.php" onsubmit="doSubmit()"> 
 
<input type="checkbox" name="boxes[]" value="Box1"> Box1
<input type="checkbox" name="boxes[]" value="Box2"> Box2
<input type="checkbox" name="boxes[]" value="Box3"> Box3
<input type="checkbox" name="boxes[]" value="Box4">.Box4
 
 
<input type="submit" name="Submit" value="Submit">
 
</form>
 
 
</body>
</html>
 

How do I set the id for "boxes"? I tried putting

id="boxes"

into those input checkbox statements, but it didn't work.

Basically, I get the results I want, but in the popup window instead of the original window because doSubmit isn't working so the popup doesn't close.

Thanks for your help!
golodog
Forum Newbie
Posts: 3
Joined: Tue Apr 29, 2008 7:21 pm

Re: passing checkbox values from a popup to a parent

Post by golodog »


Hi Bartlett,

Yeah, I posted "I am an idiot, thanks so much." Because Vlad pointed out that I had a line in test1.php to send back the text but not to send back the checkboxes. However, after putting in what I thought was missing, I still never got it to work.

If you ever figured it out, could you please let me know your solution?

Thanks!
Post Reply