...

View Full Version : stop sending empty hidden values



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;
}

head8k
02-21-2003, 04:32 PM
There's no way around this I'm afraid. If the form fields are in the HTML then they will be submitted. Your work-around is a valid way of overcoming this.

Perhaps you should consider thinking of a better way of achieving your goal which doesn't use hidden fields.

beetle
02-21-2003, 05:46 PM
You can disable each hidden element at submission. Quckie example


function disableEmpty( f )
{
for ( var i = 0; ( elem = f.elements[i] ); i++ )
{
if ( elem.type == 'hidden' and elem.value = "" )
elem.disabled = true;
}
}Where f is a reference to the form

RadarBob
02-21-2003, 06:15 PM
is there another way to do this without using as much code as i did?

YES, there is.

set the "disabled" attribute to TRUE. The field will not get sent when form is SUBMITted.



function setMarchingOrders (thefield) {

if (thefield.value == "") {
thefield.disabled = true;
}else{
thefield.disabled = false;
}
} // setMarchingOrders()



There's no way around this I'm afraid. If the form fields are in the HTML then they will be submitted. Your work-around is a valid way of overcoming this.

*Checkboxes that are not checked are not sent
*<select> <option>s that are not SELECTED are not sent
*** a select must be <select multiple> to send more than one, even when you explicitly try to script SELECTED.
* radio buttons that are not SELECTed are not sent
* text fields that are DISABLED are not sent
* hidden fields that are DISABLED are not sent.

You can script these attributes to send or not at your discresion; independent of user entry or user selection.

hogtied
02-23-2003, 07:19 PM
Great I'll give this disable thing a try

hogtied
02-23-2003, 08:20 PM
i haven't tried it yet but W3C websites states the following:

disabled -- Disables the input element when it first loads so that the user can not write text in it, or select it.
Note: Cannot be used with type="hidden"

RadarBob
02-23-2003, 08:24 PM
Note: Cannot be used with type="hidden"

All I can say is I use it sucessfully with "hidden" fields, running on IE 5.5 for Windoze.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum