Hide Null or Empty Value Fields

Posted on September 29, 2009
Filed Under Code Snippets | 1 Comment

Hiding Search Forms Fields with Null Values from the Search String.

Have you ever wanted to reduce that long confusing looking and un-necessary search string in your “GET” type forms? In our line of work we may opt to use one of three primary tools in our projects; OpenRealty, UltimateIDX, or our home grown product we use on high end projects. Regardless of product, in each instance the search forms for the MLS Quick Search uses the form action type of “GET” and in doing so it passes the form value to the URL string for the query. But what happens when you have a null value or a field with NO value? Why bother to pass fields to your search string that have no value? Over time we noted that lengthy search strings regardless of empty values or not would reduce the response time for the results substantially when compared to shorter search strings.

This script was written to help in the reduction of processing burdens placed on servers for large search forms that use the GET method for form processing. It was designed in part to reduce the SQL search timeout issue common in Real Estate MLS Search forms that rely on the “GET” method to reduce the number of fields passed in the query string. If a search form field contains a NULL or no value it will not be passed on to the GET string.

With help from coder / blogger Greg Burghardt of Fundamental Disaster we hashed out this little snippet to achieve what we were after. The code has since been modified and has about 4 different variants in use but this one is the most true to the original.

You can download the snippet in a zip file from right here; SearchForms.zip

To use this script you should add a little php to display a variable in the body tag or you can simply permanently alter the body tag as follows;
EXAMPLE:
<body onload=”enableEmptyFields(document.something);”>

You must also add an onSubmit event handler (property) belonging to a Form object as follows:
EXAMPLE:
<form action=”the-name-of-your-script.php” method=”get” onsubmit=”disableEmptyFields(this);” name=”something”>

[javascript]
/* ############################### COPYRIGHT NOTICE BEGINS #############################
@ Copyright 2008 – 2009: WP Realty Team – Chad Broussard and Jared Ritchey
@ Copyright 2008: Greg Burghardt
@ Copyright URL: http://www.wprealty.org and http://fundamentaldisaster.blogspot.com/
@ Published by: Chad Broussard and Jared Ritchey
@ Scripting: In part by http://fundamentaldisaster.blogspot.com/ and Jared Ritchey Design
@ License: GNU / GPL
@ File Name: searchforms.js
############################### COPYRIGHT NOTICE ENDS ############################# */
/*
@ DISCLAIMER AND USAGE AND LICENSE:
You are free to use this script for whatever reason you so desire providing you leave the copyright details above in
tact as required and you do not redistribute this code rebranded as your own work. You are free to include this
code in any commercial or non-commercial application without further permission or obligation from WPRealty or
Chad Broussard. You may NOT republish this code absent of this copyright notice. LICENSED AS GNU/GPL Open Source
with provisions.
*/

function enableEmptyFields(form) {
var i = 0;
var field;
var j = 0;
var opt;
while (field = form.elements[i++]) {
switch (field.nodeName) {
case "input":
switch (field.type) {
case "checkbox":
case "radio":
field.disabled = !field.checked;
break;
default:
field.disabled = false;
break;
}
break;
case "select":
if (field.multiple) {
j = 0;
while (opt = field.options[j++]) {
opt.disabled = !opt.selected;
}
} else {
field.disabled = false;
}
break;
default:
field.disabled = false;
break;
}
}
}
function disableEmptyFields(form) {
var i = 0;
var field;
var j = 0;
var opt;

while (field = form.elements[i++]) {
switch (field.nodeName) {
case "input":
switch (field.type) {
case "checkbox":
case "radio":
field.disabled = !field.checked;
break;
default:
field.disabled = (field.value.length > 0) ? false : true;
break;
}
break;
case "select":
if (field.multiple) {
j = 0;
while (opt = field.options[j++]) {
opt.disabled = !opt.selected;
}
} else {
field.disabled = (field.value.length > 0) ? false : true;
}
break;
default:
field.disabled = (field.value.length > 0) ? false : true;
break;
}
}
}
[/javascript]

Comments

Leave a Comment

If you would like to make a comment, please fill out the form below.

You must be logged in to post a comment.


TEMP CODE: GDHXCDX17249629