Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old July 1st, 2008, 09:37 PM
Familiar Sight
 
Join Date: Sep 2007
Posts: 209
Default hidden fields contain value, but then lose value on submission

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.

[code=html]
<input name="button5" type="button" class="Buttons" id="button5" value="On File" OnClick="setup_req('cpr_onfile',1);"/>
<input type="hidden" name="cert_onfile"/>

.....

<cfform action="submit.cfm" method="post" name="perf_form" id="perf_form" onsubmit="return form_submission(); return false;">

....

<input type="submit" name="button13" id="button13" value="Submit" class="Buttons" onClick="return form_submission();return false;"/>
[/code]

[code=javascript]
function setup_req(req,val){
//This function saves the data when the user selects "On File" or "NA"
req_ele=document.getElementsByName(req)
req_ele.value=val
}

function form_submission(){
alert("Validating Fields")
var testing = document.getElementsByName('cert_onfile')
alert(testing.value)
alert(testing)
return true;
}
[/code]

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?
Reply
  #2  
Old July 1st, 2008, 09:49 PM
Familiar Sight
 
Join Date: Sep 2007
Posts: 209
Default

Nevermind, I should be using getElementById rather than getElementsByName
Reply
  #3  
Old July 1st, 2008, 10:54 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,593
Default

Even if you do, if you've not set the ID, it's not going to work.
Reply
  #4  
Old July 2nd, 2008, 02:18 PM
Familiar Sight
 
Join Date: Sep 2007
Posts: 209
Default

Quote:
Originally Posted by acoder
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.
Reply
  #5  
Old July 2nd, 2008, 03:10 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,593
Default

A couple of points:
  1. You could've use document.getElementsByTagName like this:
    Code:
    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().
Reply
  #6  
Old July 2nd, 2008, 05:03 PM
Familiar Sight
 
Join Date: Sep 2007
Posts: 209
Default

Quote:
Originally Posted by acoder
A couple of points:
  1. You could've use document.getElementsByTagName like this:
    Code:
    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.
Reply
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles