There are a number of ways to make the forms on your website more user friendly. This article will explain one way of doing this, which eliminates a page reload and replaces it with an image generated on the server.
We loop through all the form controls and determine whether the current form control is a text field. For larger forms with more types of elements (radio buttons, select menus, etc.), you could add additional type checks. Keep in mind that, since we will be using a GET request, this technique is best used with small forms that won't be transmitting a lot of data.
If the current element in the loop is a text field, we determine whether we have a validating function. If so, the values are sent to the function. Logically this validation function should prevent submission, and perhaps highlight the fields that need changing.
val = fobj.elements[i].value;
If there is no validation function specified, we simply store the value temporarily in the variable "val".
str += fobj.elements[i].name +
"=" + escape(val) + "&";
str = str.substr(0,(str.length - 1));
The loop will put an unnecessary "&" character at the end of the query string. Once the loop finishes assembling the query string, we clip the extra character off the end of the query string and return the completed string.