Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

open a new window with a form in it, make a selection in the form and submit, format the variable then pass it back to the original wndow

Posted on 2004-12-01
10
Medium Priority
?
342 Views
Last Modified: 2008-02-01
Hi all, real bobby dazzler here.

I have a complicated form which creates a sql query. Everything works but the client has requested a change.

At the moment there is a list of postcodes areas as tick boxes. This allows the user to tick the postcode areas that they want to search and when the form is submitted all the ticked tick boxes are gathered together and an array is created. This works perfectly.

The change is that instead of 125 tick boxes (all created dynamically) the client now wants a text box so that the user just types in the required postcodes eg AB,ST,E.... and so on BUT if they don't know the postcodes then we have a link to a popup box containing the 125 dynamically created tickboxes. The user then ticks the required postcodes and clicks submit. The ticked tick boxes are then gathered together, formatted and sent back to the original form and put into the textbox, closing the popup on the way.

So basically I have all the code but the problem is that when I try to combine the javascript open window, get info, pass info back to opener, close window (I can do this easily, and have it working) with my 125 tickboxes then it doesn't work

some code

OK THIS OPENS THE POPUP WINDOW
[CODE]
<script language="JavaScript">
function restart(){
document.eroll_form.post2.value = post2;
mywindow.close();
}
</script>
<form action="" method="post" name="eroll_form" id="eroll_form">
  <input type="text" name="post2" value="">
POST 2 <a href="javascript:showPost2()">Show Post 2</a>
</form>
[/CODE]

THIS IS THE POPUP WINDOW - I'm not using dynamic tickboxes at this point, just a sample of 3 for speed.
[code]
<script language="JavaScript">
function generateArray(){
<%
post_code = ""
post_code = Split(Request.form("postcode2"),",")
For intX = 0 to UBound(post_code)
    post_code(intx) ="'"&trim(post_code(intx))&"'"
Next
postcode_array = Join(post_code,",")
%>
opener.post2 = postcode_array;
opener.restart();
self.close();
}
</script>



<html>
<head>
<title>post2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="post_form">
<table width="90%" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td>AB</td>
    <td><input name="postcode2" type="checkbox" id="ab" value="ab"></td>
  </tr>
  <tr>
    <td>ST</td>
    <td><input name="postcode2" type="checkbox" id="ST" value="ST"></td>
  </tr>
  <tr>
    <td>E</td>
    <td><input name="postcode2" type="checkbox" id="E" value="E"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><input type="button" value="Submit" onClick="javascript:generateArray();"></td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>
[/code]


AND THIS IS THE FORMAT AND PASSBACK CODE THAT I AM HAVING THE PROBLEM WITH FROM THE ABOVE CODE
[code]
<script language="JavaScript">
function generateArray(){
<%
post_code = ""
post_code = Split(Request.form("postcode2"),",")
For intX = 0 to UBound(post_code)
    post_code(intx) ="'"&trim(post_code(intx))&"'"
Next
postcode_array = Join(post_code,",")
%>
opener.post2 = postcode_array;
opener.restart();
self.close();
}
</script>
[/code]

now the bit betwen the <% %> is as it was from the asp page that I was using originally because that works(i know that things like the <% %> need to come out), I have butchered it so much that I thought it best to revert to what works. The problem is converting that into a working javascript

thanks in advance

Andy
0
Comment
Question by:abenbow
  • 5
  • 4
10 Comments
 

Author Comment

by:abenbow
ID: 12713700
sorry, I just wanted to clarify. If I just set opener.post2 = postcode_array; to opener.post2 = 'st'; and remove all the code between <% %> then it works, passing st back into the text box.


Andy
0
 
LVL 3

Expert Comment

by:pnedic
ID: 12717003
Hi Andy,

so do you still need help or you've fixed the problem

Regards

Peter
0
 

Author Comment

by:abenbow
ID: 12717221
still need help :(
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:pnedic
ID: 12717587
OK,
since now I'm in hurry, I'll tel you some principles how it should work. Tomorrow I'll make some simple examples and post it.

1. On the parent form (window), you need to add a hidden field like <INPUT TYPE="hidden" name="hdval" ...
2. On child form (window), on click or onSelect or onSubmit event (what ever you use to set selected post code), you need to set the value of hidden field on the PARENT form to the selected value as :

//set value of hidden field on parent form
window.parent.opener.document.parFormName.hdval.value = selected_value;
//then set FOCUS to the some visible object on parent form -> !!!
window.parent.opener.document.parFormName.someButton.focus(); //-> !!!
//Finally close child form
window.close();

3. Define onFocus event to the someButton visible element on parent form to activate function that will just do : document.formName.submit();
Of course, you will need to preserve this function to NOT SUBMIT form if value of hidden field is null or empty string

Basically this is the concept - but this is 100% sure it will work. I have seen many other solutions that sometimes works and sometimes does not (but this is "beaty" of JavaScript)

Regards

Peter
0
 

Author Comment

by:abenbow
ID: 12718853
okaay

what you are saying makes sense but the problem that i am having is getting the ticked boxes out, i haven't got a problem passing something to the opener, it the act of gathering the data from the form and formatting it that is the problem or its the act of converting my asp code into javascript

ie
 opener.post2 = 'st';  works

but i want to create an array of ticked boxes called  postcode_array and then use

opener.post2 =  postcode_array;

the problem is the array :)

this asp code will create the array

post_code = ""
post_code = Split(Request.form("postcode2"),",")
For intX = 0 to UBound(post_code)
    post_code(intx) ="'"&trim(post_code(intx))&"'"
Next
postcode_array = Join(post_code,",")


but not in the javascript, what i need is a way of doing what the asp does in javascript or a way of running the asp when i click submit then running the javascript to pass it to the opener and close the window

Andy
0
 
LVL 3

Expert Comment

by:pnedic
ID: 12723875
Hm,
it seems I did not understend what you need (sorry).

I think now I understand :
In popup window you will have 125 values, each of them associated with one check box. Then you select some of them and onSubmit, you need to pass back information about WHAT check boxes were checked.
ie, you need to pass values of selected items back to opener

Am I correct ?

PS. If I'm correct, then it should not be problem

0
 
LVL 3

Expert Comment

by:pnedic
ID: 12723925
One more question :

How do you make this list of of postcodes in the popup window :
1. does popup window have hardcoded table with postcodes and check boxes, or
2. you pass list of postcodes FROM parent window to the child window (as an array)

As I see from your code, you do option 2.

If you do option 2, I do not see some necessity of this, but I'm not going to comment your needs :)
0
 
LVL 9

Assisted Solution

by:ftaco96
ftaco96 earned 1000 total points
ID: 12727217
try this:

function generateArray(){
var postcodes;
var len = document.postcode.length;

for (i=0; i < len; i++) {
    if (document.postcode[i].checked) {
        postcodes += document.postcode[i].value + ' ';
    }
}
opener.post2 = postcodes;
opener.restart();
self.close();
}
<body>
    <input type=checkbox id=postcode value="st"> st
    <input type=checkbox id=postcode value="ab"> ab
    <input type=checkbox id=postcode value="e"> e
</body>

The key to iterating through these is the fact that the id is the same for each checkbox, creating a collection of checkboxes named postcode. Keep in mind that if you only have one postcode checkbox, the script will error. Shouldn't be a problem though, if you always have more than one postcode in the page.
0
 

Author Comment

by:abenbow
ID: 12731603
hi guys

sorry just got chance to look at this properly

 pnedic

the postcodes are generated dynamically in the popup window - there are 125 at the moment, I'm buggered if I'm creating that many tick boxes :o

 ftaco96

Thx for that, I'll give that a go tonight, ta



Andy
0
 
LVL 3

Accepted Solution

by:
pnedic earned 1000 total points
ID: 12733817
HI Andy

this what you can see from the "ftaco96" message, is OK.

I'll give you almost the same script (at child's window), which will provide you creating a string that later you can parse using VB commands for creating array from string. Also this script contains check number of check boxes

so here is the script

<SCRIPT LANGUAGE="JavaScript">
function generateArray() {

      var allChecks = window.document.post_form.postcode2;
      var len = "";
      var selected_object;
                var selected_chk = "";
      
      //Check how many check boxes are on form
      if (allChecks != null) {
            if (allChecks.length == null) {
                  len = 1;                  //Only one check box
            } else {
                  len = allChecks.length;      //more than one check boxes
            }
      }

      for (var i = 0; i < len; i++) {
            if (len > 1) {
                  selected_object = allChecks(i);
            } else {
                  selected_object = allChecks
            }
            if (selected_object.checked == true) {
                  selected_chk += selected_object.value + ",";
            }
      }
                //Remove last comma character
      selected_chk = selected_chk.substring(0, selected_chk.length - 1);

      window.parent.opener.document.parent_form.hidden_val_name.value = selected_chk;

      //Optionaly if you need to execute submit on parent form when closing child form
      //you need to set focus on some visible element on parent form (see my old mail)
      //window.parent.opener.document.parent_form.someButton.focus();
      
                window.close();
}
</SCRIPT>

Regards
Peter
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question