 |

July 1st, 2008, 09:37 PM
|
|
Familiar Sight
|
|
Join Date: Sep 2007
Posts: 209
|
|
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?
|

July 1st, 2008, 09:49 PM
|
|
Familiar Sight
|
|
Join Date: Sep 2007
Posts: 209
|
|
Nevermind, I should be using getElementById rather than getElementsByName
|

July 1st, 2008, 10:54 PM
|
 |
Site Moderator
|
|
Join Date: Nov 2006
Location: UK
Posts: 12,593
|
|
Even if you do, if you've not set the ID, it's not going to work.
|

July 2nd, 2008, 02:18 PM
|
|
Familiar Sight
|
|
Join Date: Sep 2007
Posts: 209
|
|
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.
|

July 2nd, 2008, 03:10 PM
|
 |
Site Moderator
|
|
Join Date: Nov 2006
Location: UK
Posts: 12,593
|
|
A couple of points: - 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.
- There's no need to return false in the onclick/onsubmit since you're already returning with form_submission().
|

July 2nd, 2008, 05:03 PM
|
|
Familiar Sight
|
|
Join Date: Sep 2007
Posts: 209
|
|
Quote:
|
Originally Posted by acoder
A couple of points: - 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.
- 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.
|
 |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
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.
|