469,953 Members | 2,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. <form action="Defect.asp?WCI=SignOn&WCE=frmSignOn" method="post" name="form1">
  2. <input name='button' type='hidden'>
  3. <ul class='action'>
  4. <li class='action'>
  5. <a class='action' href="javascript: {document.form1.button.value='Login'; document.form1.submit()};">
  6. LOGIN
  7. </a>
  8. </li>
  9. </ul>
  10. </form>
  11.  
Jan 29 '07 #1
17 21142
acoder
16,027 Expert Mod 8TB
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="javascript: submitForm();">[/HTML]
and your Javascript code:
Expand|Select|Wrap|Line Numbers
  1. function submitForm() {
  2.  document.form1.button.value='Login';
  3.  document.form1.submit()
  4. }
Jan 29 '07 #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="javascript: submitForm();">[/HTML]
and your Javascript code:
Expand|Select|Wrap|Line Numbers
  1. function submitForm() {
  2.  document.form1.button.value='Login';
  3.  document.form1.submit()
  4. }

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.
Jan 29 '07 #3
acoder
16,027 Expert Mod 8TB
Then just pass the 'Login' value or whatever value you wish to the submitForm function:
Expand|Select|Wrap|Line Numbers
  1. submitForm('Login')
then in your code:
Expand|Select|Wrap|Line Numbers
  1. function submitForm(buttontext) {
  2. document.form1.button.value=buttontext;
  3. ...
Jan 29 '07 #4
Then just pass the 'Login' value or whatever value you wish to the submitForm function:
Expand|Select|Wrap|Line Numbers
  1. submitForm('Login')
then in your code:
Expand|Select|Wrap|Line Numbers
  1. function submitForm(buttontext) {
  2. document.form1.button.value=buttontext;
  3. ...
Thank you very much - that looks like it might well do the job. I'll let you know.
Jan 29 '07 #5
Then just pass the 'Login' value or whatever value you wish to the submitForm function:
Expand|Select|Wrap|Line Numbers
  1. submitForm('Login')
then in your code:
Expand|Select|Wrap|Line Numbers
  1. function submitForm(buttontext) {
  2. document.form1.button.value=buttontext;
  3. ...
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.
Jan 29 '07 #6
acoder
16,027 Expert Mod 8TB
It should work. Give the url or the full code here.
Jan 29 '07 #7
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.
Jan 30 '07 #8
acoder
16,027 Expert Mod 8TB
Why not just use the reset() method on the form for this?
Jan 30 '07 #9
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
Jan 30 '07 #10
acoder
16,027 Expert Mod 8TB
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.
Jan 30 '07 #11
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

Expand|Select|Wrap|Line Numbers
  1. <!-- In the <head> of the page-->
  2. <script language='javascript'>
  3. function submitform(buttontext) {document.form1.button.value=buttontext;document.form1.submit()}</script>
  4.  
  5. <!-- 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"))->
  6. <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>
  7.  
Jan 31 '07 #12
acoder
16,027 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. function submitform(buttontext) {
  2.  document.form1.button.value=buttontext;
  3.  if (buttontext=='Reset') document.form1.reset();
  4.  else document.form1.submit();
  5. }
So, it checks for 'Reset' and instead of submitting the form, it resets the form.
Jan 31 '07 #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:
Expand|Select|Wrap|Line Numbers
  1. function submitform(buttontext) {
  2.  document.form1.button.value=buttontext;
  3.  if (buttontext=='Reset') document.form1.reset();
  4.  else document.form1.submit();
  5. }
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.
Jan 31 '07 #14
acoder
16,027 Expert Mod 8TB
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.
Jan 31 '07 #15
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.
The value of the hidden button is checked in server side code and processing occurs according to its value. It works in IE but it appears that when the javascript runs in Firefox, it fails to set the value of the hidden input element called 'button' before the form is submitted so that my app cannot tell, which button has been clicked. The website is a VB6 web class application and the server side code looks like this:

Expand|Select|Wrap|Line Numbers
  1. dim str_RequiredAction as String
  2.  
  3. str_RequiredAction = request.Form("Button")
  4.  
  5. 'With an IE browser running on the client, the request.form("Button") 
  6. 'call correctly sets the str_RequiredAction variable but in Firefox it doesn't 
  7.  
  8. Select case str_RequiredAction
  9.  
  10. Case is = "Post"
  11. 'Server side validation and ADO to write to a SQL database etc followed by redirect to appropriate page
  12.  
  13. Case is = "Delete"
  14. 'Server side delete record etc
  15.  
  16. Case is = "Cancel"
  17. 'redirect to main menu page without processing data etc
  18.  
  19. Case is = "X"
  20. 'some other action
  21.  
  22. Case is = "Y"
  23. 'Yet another action
  24.  
  25. End Select
  26.  
  27.  
Jan 31 '07 #16
The value of the hidden button is checked in server side code and processing occurs according to its value. It works in IE but it appears that when the javascript runs in Firefox, it fails to set the value of the hidden input element called 'button' before the form is submitted so that my app cannot tell, which button has been clicked. The website is a VB6 web class application and the server side code looks like this:

Expand|Select|Wrap|Line Numbers
  1. dim str_RequiredAction as String
  2.  
  3. str_RequiredAction = request.Form("Button")
  4.  
  5. 'With an IE browser running on the client, the request.form("Button") 
  6. 'call correctly sets the str_RequiredAction variable but in Firefox it doesn't 
  7.  
  8. Select case str_RequiredAction
  9.  
  10. Case is = "Post"
  11. 'Server side validation and ADO to write to a SQL database etc followed by redirect to appropriate page
  12.  
  13. Case is = "Delete"
  14. 'Server side delete record etc
  15.  
  16. Case is = "Cancel"
  17. 'redirect to main menu page without processing data etc
  18.  
  19. Case is = "X"
  20. 'some other action
  21.  
  22. Case is = "Y"
  23. 'Yet another action
  24.  
  25. End Select
  26.  
  27.  
In the end this turned out to be a stupid error on my part in the server side processing. The javascript setting of the hidden input value does work in Firefox. Sorry for time wasted.
Feb 1 '07 #17
acoder
16,027 Expert Mod 8TB
Glad you got it working. Incidentally, what could it be on the server-side that doesn't affect IE, but affects Firefox?
Feb 1 '07 #18

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Dan | last post: by
4 posts views Thread by Bosconian | last post: by
2 posts views Thread by Chris Riesbeck | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.