Chapter 4: Data Formats 127
PART I
case "textarea": formValues = encode(formValues, fieldName,
currentField.value, encoding);
break;
case "radio":
case "checkbox": if (currentField.checked)
formValues = encode(formValues, fieldName,
currentField.value, encoding);
break;
case 'select-one':
case 'select-multiple':
for (var j=0; j< currentField.options.length; j++)
if (currentField.options[j].selected)
{
formValues = encode(formValues, fieldName,
(currentField.options[j].value != null)?
currentField.options[j].value :
currentField.options[j].text , encoding);
}
break;
case "file": if (currentField.value)
return "fileupload";
else
formValues = encode(formValues, fieldName,
currentField.value, encoding);
break;
case "submit": if (currentField == trigger)
formValues = encode(formValues, fieldName,
currentField.value, encoding);
break;
default: continue; /* everything else like fieldset you don't want */
}
}
}
if (trigger && trigger.type == "image" && trigger.name)
{
/* this is where we need to insert the trigger image information */
formValues = encode(formValues, trigger.name + ".x", x, encoding);
formValues = encode(formValues, trigger.name + ".y", y, encoding);
formValues = encode(formValues, trigger.name, trigger.value, encoding);
}
return formValues;
}
To use the following function, you must supply minimally a reference to the form; the
other parameters are optional but will often be used.
var payload = serializeForm(document.getElementById("contactForm"));
To make the function flexible, the next parameter is an optional encoding field that
allows the specification of the desired encoding type for the data collected out of the form.
When not specified or set to some other encoding value than specified, such as the value
“default”, it defaults to the standard x-www-form-urlencoded. However, we define