By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,681 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,681 IT Pros & Developers. It's quick & easy.

hidden fields contain value, but then lose value on submission

100+
P: 219
I'm having a strange issue with a form I'm developing. I'm saving some values to hidden fields when a user clicks a button. I setup a function which gets ran on submission of the form, but I also placed this function on my submission button when clicked.

Expand|Select|Wrap|Line Numbers
  1. <input name="button5" type="button" class="Buttons" id="button5" value="On File" OnClick="setup_req('cpr_onfile',1);"/>
  2. <input type="hidden" name="cert_onfile"/>
  3.  
  4. .....
  5.  
  6. <cfform action="submit.cfm" method="post" name="perf_form" id="perf_form" onsubmit="return form_submission(); return false;">
  7.  
  8. ....
  9.  
  10. <input type="submit" name="button13" id="button13" value="Submit" class="Buttons" onClick="return form_submission();return false;"/>
  11.  
Expand|Select|Wrap|Line Numbers
  1. function setup_req(req,val){
  2.     //This function saves the data when the user selects "On File" or "NA"
  3.     req_ele=document.getElementsByName(req)
  4.     req_ele.value=val
  5. }
  6.  
  7. function form_submission(){
  8.     alert("Validating Fields")
  9.     var testing = document.getElementsByName('cert_onfile')
  10.     alert(testing.value)    
  11.     alert(testing)
  12.     return true;
  13. }
  14.  
When the alert pops up when the submission button is selected, I can see the value of the hidden field. When the alert pops up when the form submission calls the "form_submission" function, the hidden field is undefined.

What could be happening when the form submission takes place that's erasing my hidden fields?
Jul 1 '08 #1

✓ answered by dmorand

Even if you do, if you've not set the ID, it's not going to work.
I set the ID and used getElementById() and it worked like a charm.

Share this Question
Share on Google+
5 Replies


100+
P: 219
Nevermind, I should be using getElementById rather than getElementsByName
Jul 1 '08 #2

acoder
Expert Mod 15k+
P: 16,027
Even if you do, if you've not set the ID, it's not going to work.
Jul 1 '08 #3

100+
P: 219
Even if you do, if you've not set the ID, it's not going to work.
I set the ID and used getElementById() and it worked like a charm.
Jul 2 '08 #4

acoder
Expert Mod 15k+
P: 16,027
A couple of points:
  1. You could've use document.getElementsByTagName like this:
    Expand|Select|Wrap|Line Numbers
    1. document.getElementsByName("cert_onfile")[0]
    assuming that was the only element named "cert_onfile" or the first one. Having said that, it's better to use a unique ID.
  2. There's no need to return false in the onclick/onsubmit since you're already returning with form_submission().
Jul 2 '08 #5

100+
P: 219
A couple of points:
  1. You could've use document.getElementsByTagName like this:
    Expand|Select|Wrap|Line Numbers
    1. document.getElementsByName("cert_onfile")[0]
    assuming that was the only element named "cert_onfile" or the first one. Having said that, it's better to use a unique ID.
  2. There's no need to return false in the onclick/onsubmit since you're already returning with form_submission().
Thanks for those tips acoder, I'll keep them in mind. I had a feeling I didn't need the return false in my onsubmit. I'm still learning javascript, but it's coming along.
Jul 2 '08 #6

Post your reply

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