hogtied
02-21-2003, 10:29 AM
I have quite a few hidden form objects that are almost always empty values. Unless a user clicks on something. Then javascript places a value within those hidden form objects.
So when the user clicks send, I want only the hidden objects with values to be sent in the substring.
I accomplished this by using a for loop, going through every form object (document.forms[1].elements[g]), and appending the name and value of those objects to a variable. then send the variable's informtion through window.location.href
is there another way to do this without using as much code as i did?
here is what i have:
function validateForm() {
var mainForm = document.search;
// if the "(Optional)" is not cleared it will be sent and produce an error
if(mainForm.name.value == "(Optional)") {
mainForm.name.value = "";
}
var results = new String();
var mainForm = document.forms[1];
//runs through all form elements and appends only the elements with values to a link to be sent
for(i=0;i<document.forms[1].length;i++) {
//test if textbox
if(mainForm.elements[i].type == "text" && mainForm.elements[i].value != "") {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
//test if hidden box
if(mainForm.elements[i].type == "hidden" && mainForm.elements[i].value != "") {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
//test if raido button
if(mainForm.elements[i].type == "radio" && mainForm.elements[i].value != "hd" && mainForm.elements[i].checked == true) {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
//test if select box that might have values
if(mainForm.elements[i].name == "state" || mainForm.elements[i].name == "country" || mainForm.elements[i].name == "brand") {
if(mainForm.elements[i].selectedIndex > 0) {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
}
//test for the rest of the select boxes that always has values
if(mainForm.elements[i].name == "num_adults" || mainForm.elements[i].name == "num_beds" ||
mainForm.elements[i].name == "num_rooms" || mainForm.elements[i].name == "doa_mm" ||
mainForm.elements[i].name == "doa_dd" || mainForm.elements[i].name == "dod_mm" ||
mainForm.elements[i].name == "dod_dd") {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
}
window.location.href="http://www.website.com/power_results.cgi?" + results;
}
So when the user clicks send, I want only the hidden objects with values to be sent in the substring.
I accomplished this by using a for loop, going through every form object (document.forms[1].elements[g]), and appending the name and value of those objects to a variable. then send the variable's informtion through window.location.href
is there another way to do this without using as much code as i did?
here is what i have:
function validateForm() {
var mainForm = document.search;
// if the "(Optional)" is not cleared it will be sent and produce an error
if(mainForm.name.value == "(Optional)") {
mainForm.name.value = "";
}
var results = new String();
var mainForm = document.forms[1];
//runs through all form elements and appends only the elements with values to a link to be sent
for(i=0;i<document.forms[1].length;i++) {
//test if textbox
if(mainForm.elements[i].type == "text" && mainForm.elements[i].value != "") {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
//test if hidden box
if(mainForm.elements[i].type == "hidden" && mainForm.elements[i].value != "") {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
//test if raido button
if(mainForm.elements[i].type == "radio" && mainForm.elements[i].value != "hd" && mainForm.elements[i].checked == true) {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
//test if select box that might have values
if(mainForm.elements[i].name == "state" || mainForm.elements[i].name == "country" || mainForm.elements[i].name == "brand") {
if(mainForm.elements[i].selectedIndex > 0) {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
}
//test for the rest of the select boxes that always has values
if(mainForm.elements[i].name == "num_adults" || mainForm.elements[i].name == "num_beds" ||
mainForm.elements[i].name == "num_rooms" || mainForm.elements[i].name == "doa_mm" ||
mainForm.elements[i].name == "doa_dd" || mainForm.elements[i].name == "dod_mm" ||
mainForm.elements[i].name == "dod_dd") {
results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
}
}
window.location.href="http://www.website.com/power_results.cgi?" + results;
}