HomeArticles Avoid submitting empty fields to server
Avoid submitting empty fields to server
Written by Administrator
Saturday, 27 October 2007
This little javascript function with customization ofcourse will submit to the server ony fields with values in them. Example..
[searchform]
name -> empty
email ->
age -> empty
normal query string - search.php?name=&email=address%40host.com&email&age=
with this functionality - search.php?email=address%40host.com
You can also use this with some tweaks if for some reason you wanted to submit information for several different forms.
Javascript Code:
function doSearch(){ var s = document.getElementById('s_form') var isvalid = new Array() var v_hash = new Array()
for(i=0;i if(s.elements[i].type=='text' && s.elements[i].value.length > 0 ){ isvalid.push(s.elements[i].name) v_hash.push(s.elements[i].value) } } switch(isvalid.length){ case 0: alert("You must enter a keyword in atleast one field") default: filter_form = document.createElement('FORM') filter_form.setAttribute('name','filter_search') filter_form.setAttribute('action','search.php') filter_form.setAttribute('method','get')