Trim whitespace on search input field

3 posts by 2 authors in: Forums > CMS Builder
Last Post: May 22, 2018   (RSS)

By gversion - May 22, 2018

Hello,

I am using the "fieldname_keyword" search type on my form and I have noticed that if a user searches for "television " (with a space at the end of the search phrase) then no results are returned. If the user searches for "television" then the correct results are returned.

What's the best way to handle this? Should I be trimming the whitespace using jQuery? Should I be using some PHP that removes any leading and trailing whitespace?

Any advice and sample code would be greatly appreciated!

Regards,

Greg

By leo - May 22, 2018

Hi Greg,

I think it doesn't need to be modified, because if the user input "television " he might be looking for records like "television cable" or anything that contains television but not exactly television.

However, if you still want to modify the search query, rtrim() may be the function you need: http://php.net/manual/en/function.rtrim.php

Thanks,

Leo - PHP Programmer (in training)
interactivetools.com

By gversion - May 22, 2018

Thanks, Leo!

I was trying to get the form to remove the trailing whitespace using JS with the following code:

    <script>
      $('#search_submit_button').click(function(){
      var text = $('#search-desktop').val();
      text = text.trim();
      $('#search-desktop').val(text);
      $("#header-search").submit();
      });
    </script>

Seems to be working ok...!

Regards,

Greg