Back
Question
Asked

PHP search functionality question.

Hey all,

Hope everyone is doing great. I'm creating a PHP application with a search functionality that provides dropdown suggestions similar to Google. Currently, as I type the query, it sends the search query incrementally with each keystroke. For example: `query: t, query: te, query: tes, query: test`. It's working fine, but I'm wondering if there's a more efficient way to handle this.

Here is a screenshot for your reference.

image.png 41 KB


Hi Shazeb,

This is how all auto complete components that I'm seen work. Why do you think it needs optimizing? If it's slow, maybe there's a database index optimization needed. 😊

Hey, thanks for the reply.

I was thinking it's making too many requests. That's why I thought there must be a way to use sockets or something similar for continuous data streaming. But I'm new to this, so I don't know for sure. That's why I'm asking here for advice from experienced people.

Maybe worth adding throttling or debouncing on the client so that it sends the request after a few characters have been typed rather than sending a request on each character. That's what I remember is a standard practice with those searches.

Also, you might want to find a JS library to easily display those search results, or use something like Alpine.js with Tailwind and DIY.

Just add a 150ms delay before sending the request.

And check this out. It might be helpful:

ux.symfony.com/live-component

I'm using it in all my projects. No custom JS required.

If you want to make your own, at least you can find some approaches there.