Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with form in window popup

Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#1: Aug 28 '09
Hi all.

I have this JavaScript function that copy the value ( selected in a select open in a window popup), in the hidden main form field.

I need this changes:

1) If the popup window is not requested by the user, the hidden main form field take value = 0;
2) If the window is open to force the user to make a choice within the select.

Can you help me?
Thanks

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. <!--
  3.  
  4. function setValue(selObj)
  5. {
  6.  
  7.   if (!window.opener || selObj.selectedIndex <= 0) return;
  8.  
  9.   var vl = "";
  10.   var opts = selObj.options;
  11.  
  12.   for ( n = 0 ; n < opts.length ; n++ )
  13.     {
  14.       if (opts[n].selected)
  15.         {
  16.           if (vl.length > 0) vl += ", ";
  17.           vl += opts[n].value;
  18.         }
  19.     }
  20.  
  21.   window.opener.document.Qform.NASCOSTO.value = selObj.value;
  22.  
  23.  
  24.   window.close();
  25.  
  26. }
  27.  
  28. // -->
  29. </script>

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Aug 28 '09

re: Problem with form in window popup


so just to understand that correctly:

1. when the user doesn't open the window the field should be set to 0? that is the default value of the field?

2. when he/she once opens the window ... then a selection has to be made and the window couldn't be closed without a selection?
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#3: Aug 28 '09

re: Problem with form in window popup


Quote:
1. when the user doesn't open the window the field should be set to 0? that is the default value of the field?
The default value of the field is NULL;

Quote:
2. when he/she once opens the window ... then a selection has to be made and the window couldn't be closed without a selection?
Exactly...

Try this:
http://www.avia-it.com/public/js/gits.htm

thanks...
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#4: Aug 29 '09

re: Problem with form in window popup


for the first issue you might just set the default to 0 or map it in your code because you know that the value NULL translates to 0 in your case.

for the second issue you might use the onbeforeunload handler of the window to react properly to the user's actions ... have a look here for the usage ...

kind regards
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#5: Aug 29 '09

re: Problem with form in window popup


for the first issue it's ok...

for the second issue I don't understand the user's actions in my case... sorry...

If the window is open to force the user to make a choice within the select...

Can you tell me how resolve the problem ?

Regards
Viki
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#6: Aug 29 '09

re: Problem with form in window popup


just use the onbeforeunload-event to validate whether the user has selected something or not ... and then do whatever you want in case the user didn't select something ... give a warning, avoid to close the window or whatever ... you could even use a custom dhtml-window so that you don't need to fiddle with different windows and close buttons etc.
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#7: Aug 29 '09

re: Problem with form in window popup


OK, but can you tell me how I modify my code?
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#8: Aug 29 '09

re: Problem with form in window popup


come on :) ... you should know that in this forum you should show some efforts to solve it first ... so try to implement the onbeforunload handler first ... the link shows you how you should use it ...

kind regards
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#9: Aug 30 '09

re: Problem with form in window popup


OK, I understand...

I write this code... working but I'am not sure it is a conform solution to the problem ...

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <title>Popup</title>
  5.  
  6. <script language="javascript" type="text/javascript"> 
  7. <!--
  8.  
  9.  
  10. function conferma(){
  11.  
  12.   if(document.Qform.ELEMENTO.value.length <= 0)
  13.   {
  14.      alert("Compila tutti i campi");
  15.   }
  16.   else
  17.   {
  18.      document.Qform.submit();
  19.   }
  20.  }
  21.  
  22.  
  23. function setValue(selObj)
  24.  
  25.   if (!window.opener || selObj.selectedIndex <= 0) return;
  26.  
  27.   var vl = "";
  28.   var opts = selObj.options;
  29.  
  30.   for ( n = 0 ; n < opts.length ; n++ )
  31.     {
  32.       if (opts[n].selected)
  33.         {
  34.           if (vl.length > 0) vl += ", ";
  35.           vl += opts[n].value;
  36.         }
  37.     }
  38.  
  39.  
  40.      window.opener.document.Qform.NASCOSTO.value = selObj.value;
  41.      window.close();
  42.  
  43. }
  44.  
  45. // -->
  46. </script>
  47.  
  48. </head>
  49.  
  50. <body>
  51.  
  52. <form name="Qform">
  53.  
  54.  
  55.   <select name="ELEMENTO" size="1" onchange="setValue(this);">
  56.   <option value="">Selezionare ELEMENTO</option>
  57.  
  58.     <option value="ALTRO">ALTRO</option>
  59.  
  60.     <option value="BLU">BLU</option>
  61.  
  62.     <option value="GIALLO">GIALLO</option>
  63.  
  64.     <option value="ROSSO">ROSSO</option>
  65.  
  66.     <option value="VERDE">VERDE</option>
  67.  
  68.   </select>
  69.  
  70.       <p align="center">
  71.       <a href="javascript:void(0);" onclick="conferma()">
  72.       <input type="image"src="products.gif" border="0" align="middle" name="I3"></a>
  73.  
  74. </form>
  75.  
  76. </body>
  77. </html>
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#10: Aug 30 '09

re: Problem with form in window popup


ok ... that would validate the input when the link is clicked. so now when you don't need to check when the user just closes the window without a selection (through the close-button - X-button of the window's title bar or with another action like alt+F4) then it would be sufficient. otherwise the mentioned onbeforeunload-event could help ...

kind regards
Reply


Similar JavaScript / Ajax / DHTML bytes