avoiding empty submissions
I use Atomz search to allow users to search my site and the weekly report always has the same top query. It is (drum roll please):
blank query
I have no idea why someone would search for nothing, because you get just that as a result, but search for nothing they did.
I decided to put a stop to it. In the header I added a Javascript function that checks the value of the search field and if blank, pops up a message asking the user to please enter a search phrase. Here’s the code to do that.
<script type="text/javascript" language="Javascript"> //<!-- function validateSearch() {if ( document.search.sp_q.value == "" )if ( document.search.sp_q.value.match( /^\s*$/ ) != null ) { alert( "Please enter a search phrase" ); return false; } return true; } //--> </script>
The Javascript method is executed when the user hits the search button by adding the bold text.
<form name="search" method="get" action="http://search.atomz.com/search/" onSubmit="return validateSearch();"> <input type=text size=30 name="sp_q"><br> <input type=submit value="Search this site"> </form>
Blank queries still show up occasionally, but they’re probably from users who have Javascript disabled. An even better solution would be for the server to make sure the search isn’t blank too, but since I don’t have access to the server-side code, this is the best I can do.
As a jerk, I could not avoid typing in a space into the search field and then pressing search. Sorry, my willpower is weak.
It is amazing (and laughable) where cleverness is required!
Drat. That chaps my hide.
Please see the altered version of the function that checks for spaces as well.
Oh do you know how much of an urge I have to do a bunch of blank searches now?
Heh heh heh. Try it. They will be no match for my snazzy (slightly modified) function.
Flaw in your plan? At your site you need to enter some text, then the results of the query are displayed on the atomz site. On the results page there is another query box, which _allows_ for a no text search. Do those show in your logs as well?
Chad: Excellent point – maybe that’s why I keep getting blank queries. I modified the search results template to also not allow blank queries. The only problem is it no longer displays the last query in the search box, but I’m willing to live with that.