Javascript set hidden input value does not work in Firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SimonWilding
    New Member
    • Jan 2007
    • 10

    #1

    Javascript set hidden input value does not work in Firefox

    I am using a line of javascript that both sets the value of a hidden input element (name='button') and then submits the form. The destination of the post can then pick up the value of the hidden field (request.form(" button")) and process accordingly. I'm using this to detect which button has been clicked. The buttons are built as links because they can detect clicks and because I want a consistent appearance with drop down menus that I've used elsewhere in the site. This is simple to implement and works perfectly in IE but not in firefox where it submits but does not pick up the value of the hidden field (i.e. 'Login' below). Can anybody help with a suggestion that will make it work in firefox and save me the task of coding submit buttons separately on a very large number of pages. If so, I would be extremely grateful. Here's the code:

    Code:
    <form action="Defect.asp?WCI=SignOn&WCE=frmSignOn" method="post" name="form1">
    <input name='button' type='hidden'>
    <ul class='action'>
    <li class='action'>
    <a class='action' href="Javascript: {document.form1.button.value='Login'; document.form1.submit()};">
    LOGIN
    </a>
    </li>
    </ul>
    </form>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TheScripts.

    Don't use brackets in the href attribute for your link. Also, define a function instead of putting code directly in your link.
    [HTML]<a class='action' href="javascrip t: submitForm();">[/HTML]
    and your Javascript code:
    Code:
    function submitForm() {
     document.form1.button.value='Login';
     document.form1.submit()
    }

    Comment

    • SimonWilding
      New Member
      • Jan 2007
      • 10

      #3
      Originally posted by acoder
      Welcome to TheScripts.

      Don't use brackets in the href attribute for your link. Also, define a function instead of putting code directly in your link.
      [HTML]<a class='action' href="javascrip t: submitForm();">[/HTML]
      and your Javascript code:
      Code:
      function submitForm() {
       document.form1.button.value='Login';
       document.form1.submit()
      }

      Thanks for your help but I have numerous buttons on the form and need each to post a different value to the post destination. Its much easier therefore to write the javascript inline - partly because I'm using a VB function to build the code, which will be placed on 100+ pages.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Then just pass the 'Login' value or whatever value you wish to the submitForm function:
        Code:
        submitForm('Login')
        then in your code:
        Code:
        function submitForm(buttontext) {
        document.form1.button.value=buttontext;
        ...

        Comment

        • SimonWilding
          New Member
          • Jan 2007
          • 10

          #5
          Originally posted by acoder
          Then just pass the 'Login' value or whatever value you wish to the submitForm function:
          Code:
          submitForm('Login')
          then in your code:
          Code:
          function submitForm(buttontext) {
          document.form1.button.value=buttontext;
          ...
          Thank you very much - that looks like it might well do the job. I'll let you know.

          Comment

          • SimonWilding
            New Member
            • Jan 2007
            • 10

            #6
            Originally posted by acoder
            Then just pass the 'Login' value or whatever value you wish to the submitForm function:
            Code:
            submitForm('Login')
            then in your code:
            Code:
            function submitForm(buttontext) {
            document.form1.button.value=buttontext;
            ...
            I recoded that so that it works exactly as you said (can point you to the URL if you like) passing the button value parameter in the href to the javascript function, which is written into the head of the page. Again, it works in IE but not in FireFox. Is there some syntactic tweak that will get it working or do I need cide each page to use regular submit buttons in Firefox. Very frustrating but thank you for you help thus far.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              It should work. Give the url or the full code here.

              Comment

              • SimonWilding
                New Member
                • Jan 2007
                • 10

                #8
                Originally posted by acoder
                It should work. Give the url or the full code here.
                Please go to www.ena-eng.org/neders4 and login with userID and Password both set to 'testu'. On the Welcome page go to the Post a Message drop down menu and select Post a Bulletin. In Firefox, the page submits when you click one of the buttons but doesn't pick up the hidden form value so a reset, which should simply clear the page fields brings up a validation error. Go to the same page in IE and a reset will correctly clear the form fields. Thanks.
                Last edited by acoder; Jan 30 '07, 03:09 PM. Reason: linked url

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Why not just use the reset() method on the form for this?

                  Comment

                  • SimonWilding
                    New Member
                    • Jan 2007
                    • 10

                    #10
                    Originally posted by acoder
                    Why not just use the reset() method on the form for this?
                    the reset method would be fine for this particular purpose but this same technique is used to determine between different actions throughout the site and the reset was just an example - I need that simple hidden form field to be set in firefox as it is in IE so that I can determine, which button the user has clicked. On the same page for example, a routine at the destination event determines whether the reset, Post or cancel and return to main menu button has been clicked and processes accordingly. It works pefectly in IE but it looks like Firefox like Netscape before it, is more limited or, at least, less flexible in its interpretation of javascript and HTML. Simon

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by SimonWilding
                      the reset method would be fine for this particular purpose but this same technique is used to determine between different actions throughout the site and the reset was just an example - I need that simple hidden form field to be set in firefox as it is in IE so that I can determine, which button the user has clicked. On the same page for example, a routine at the destination event determines whether the reset, Post or cancel and return to main menu button has been clicked and processes accordingly. It works pefectly in IE but it looks like Firefox like Netscape before it, is more limited or, at least, less flexible in its interpretation of javascript and HTML. Simon
                      What code are you using to reset the form? Resetting the form by submitting the form is not good practice. It might be ok on broadband connections, but on dialup access, when a very easy alternative is easily available on the client side, it would be inconvenient for the user.

                      I'm sure it is something in the code, rather than Firefox. Firefox is a much better browser than IE and follows standards. You should not base your opinion of Firefox on Netscape. If you post your resetting code, I'm sure there is a better alternative that will work on all browsers rather than just IE.

                      Comment

                      • SimonWilding
                        New Member
                        • Jan 2007
                        • 10

                        #12
                        Originally posted by acoder
                        What code are you using to reset the form? Resetting the form by submitting the form is not good practice. It might be ok on broadband connections, but on dialup access, when a very easy alternative is easily available on the client side, it would be inconvenient for the user.

                        I'm sure it is something in the code, rather than Firefox. Firefox is a much better browser than IE and follows standards. You should not base your opinion of Firefox on Netscape. If you post your resetting code, I'm sure there is a better alternative that will work on all browsers rather than just IE.
                        I'm only interested in making the javascript work in Firefox as it does in IE - the reset issue is not important. My website is a complex web application - on some pages there are 5 or more buttons all firing different processes - data updates, deletes, various navigation etc. I need to be able to tell which button has been clicked. Here's the code

                        Code:
                        <!-- In the <head> of the page-->
                        <script language='javascript'>
                        function submitform(buttontext) {document.form1.button.value=buttontext;document.form1.submit()}</script>
                        
                        <!-- Here's an example of a set of buttons that will be within the body of the page - I need to be able to pick up the value of the  hidden input named 'button' in the post destination with VB (i.e. str_RequiredAction = request.form("button"))->
                        <form action="Defect.asp?WCI=Feedback&WCE=frmFeedback" method="post" name="form1"><input name="button" type="hidden"><ul class='action'><li class='action'><a class='action' href="javascript: submitform('Post')">POST</a></li><li class='action'><a class='action' href="javascript: submitform('Reset')">RESET FORM</a></li><li class='action'><a class='action' href="javascript: submitform('Cancel')">CANCEL AND RETURN TO MAIN MENU</a></li>
                        Last edited by acoder; Jan 31 '07, 10:18 AM. Reason: Proper code tags

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          I meant the actual resetting code. I have already seen the code you have posted on the link you gave earlier.

                          If that code could be altered in some way instead, the reset should work fine.

                          In the meantime, you have this option too:
                          Code:
                          function submitform(buttontext) {
                           document.form1.button.value=buttontext;
                           if (buttontext=='Reset') document.form1.reset();
                           else document.form1.submit();
                          }
                          So, it checks for 'Reset' and instead of submitting the form, it resets the form.

                          Comment

                          • SimonWilding
                            New Member
                            • Jan 2007
                            • 10

                            #14
                            Originally posted by acoder
                            I meant the actual resetting code. I have already seen the code you have posted on the link you gave earlier.

                            If that code could be altered in some way instead, the reset should work fine.

                            In the meantime, you have this option too:
                            Code:
                            function submitform(buttontext) {
                             document.form1.button.value=buttontext;
                             if (buttontext=='Reset') document.form1.reset();
                             else document.form1.submit();
                            }
                            So, it checks for 'Reset' and instead of submitting the form, it resets the form.
                            Could you please read my previous post again. I'm NOT INTERESTED in a more efficient way of doing a form reset - I ONLY WANT the code to work in Firefox so that I can tell which button has been clicked - most button clicks are firing processes other than resets - that was just an example, which has led us away from my actual question.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Originally posted by SimonWilding
                              Could you please read my previous post again. I'm NOT INTERESTED in a more efficient way of doing a form reset - I ONLY WANT the code to work in Firefox so that I can tell which button has been clicked - most button clicks are firing processes other than resets - that was just an example, which has led us away from my actual question.
                              Sorry, I misunderstood. I thought you were only having problems with the reset. Reading it again, I understand now.

                              So, you don't know which button has been clicked? When I asked for the resetting code, it was still a valid question in the circumstances because nowhere does it show what code you are using to check which button has been clicked.

                              Is it server-side code? You may have to change that then. Post the code that checks which button has been clicked.

                              Comment

                              Working...