The first task is to turn the search expression into a SQL statement.
// get data from form
var sSearch = '' + Request.Form ( 'SearchText' );
if ( sSearch.length )
{
// remove all reserved characters
sSearch = sSearch.replace ( /['"%]/g, '' );
// split into array, then reform with AND
var sArray = sSearch.split ( ' ' );
var sSQLSearch = '';
for ( var i=0; i<sArray.length; i++ )
{
if ( i )
sSQLSearch += ' AND ';
sSQLSearch += 'Keywords LIKE \'% ' + sArray [ i ] + '%\'';
}
Starting from the top, we ask the form for the SearchText value and make sure there was something entered by testing that the length property is non-zero. Next, I use the replace method with a regular expression that removes any ' " % characters in the search expression to make it "SQL safe". I then use the split method to create an array of items using a space as the separator.
Lastly I loop through the array items making up the sSQLSearch string. I separate multiple words with AND, and wrap the expressions you typed with the wildcard % character. Note that I include a space after the first %, so you'll only find keywords that start with your expressions. (I prefix every word in the Keywords field with a space, even the first one)
Comments