Problem with form in window popup

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    Problem with form in window popup

    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

    Code:
    <script language="javascript" type="text/javascript">
    <!--
     
    function setValue(selObj)
    {
        
      if (!window.opener || selObj.selectedIndex <= 0) return;
     
      var vl = "";
      var opts = selObj.options;
     
      for ( n = 0 ; n < opts.length ; n++ )
        {
          if (opts[n].selected)
            {
              if (vl.length > 0) vl += ", ";
              vl += opts[n].value;
            }
        }
      
      window.opener.document.Qform.NASCOSTO.value = selObj.value;
      
       
      window.close();
    
    }
     
    // -->
    </script>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    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?

    Comment

    • viki1967
      Contributor
      • Oct 2007
      • 263

      #3
      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;

      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:


      thanks...

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        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

        Comment

        • viki1967
          Contributor
          • Oct 2007
          • 263

          #5
          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

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            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.

            Comment

            • viki1967
              Contributor
              • Oct 2007
              • 263

              #7
              OK, but can you tell me how I modify my code?

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                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

                Comment

                • viki1967
                  Contributor
                  • Oct 2007
                  • 263

                  #9
                  OK, I understand...

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

                  Code:
                  <html>
                  <head>
                   
                  <title>Popup</title>
                  
                  <script language="javascript" type="text/javascript"> 
                  <!--
                  
                  
                  function conferma(){
                  
                    if(document.Qform.ELEMENTO.value.length <= 0)
                    {
                       alert("Compila tutti i campi");
                    }
                    else
                    {
                       document.Qform.submit();
                    }
                   }
                   
                   
                  function setValue(selObj)
                  { 
                    
                    if (!window.opener || selObj.selectedIndex <= 0) return;
                   
                    var vl = "";
                    var opts = selObj.options;
                   
                    for ( n = 0 ; n < opts.length ; n++ )
                      {
                        if (opts[n].selected)
                          {
                            if (vl.length > 0) vl += ", ";
                            vl += opts[n].value;
                          }
                      }
                   
                     
                       window.opener.document.Qform.NASCOSTO.value = selObj.value;
                       window.close();
                   
                  }
                   
                  // -->
                  </script>
                   
                  </head>
                   
                  <body>
                   
                  <form name="Qform">
                  
                   
                    <select name="ELEMENTO" size="1" onchange="setValue(this);">
                    <option value="">Selezionare ELEMENTO</option>
                   
                      <option value="ALTRO">ALTRO</option>
                   
                      <option value="BLU">BLU</option>
                   
                      <option value="GIALLO">GIALLO</option>
                   
                      <option value="ROSSO">ROSSO</option>
                   
                      <option value="VERDE">VERDE</option>
                   
                    </select>
                    
                        <p align="center">
                        <a href="javascript:void(0);" onclick="conferma()">
                        <input type="image"src="products.gif" border="0" align="middle" name="I3"></a>
                   
                  </form>
                   
                  </body>
                  </html>

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    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

                    Comment

                    Working...