When a user does a search on my website, and there is only one entry, then I want to redirect the user to the search result. The current way that I am doing this is poor. Here is how I am currently doing this:
If there is only one search result on the search result page, then I render a hidden input with an ID of “redirect” and a value of the link to redirect to. In the javascript, if the hidden input with ID “redirect” exists, then the user is redirected to the value of the element.
It’s a poor way to do this because the single search result is loaded first, so the user actually sees that there is one search result. Then, it loads after, say, 3 seconds.
Is there a better way to do this?
You could use the header() PHP function to redirect if there is only 1 result.