Hey everyone, I'm a beginner full-stack developer and I'm working on a mobile app. During testing, I realized that if a user clicks the buttons quickly, it sends multiple requests to my backend PHP API. This is causing issues like executing multiple "mysqli_commit" operations when the database changes. I'm looking for safe methods to avoid this situation. Any suggestions would be appreciated! And feel free to point out any mistakes in my understanding as well, I'm open to feedback.
5 Answers
During a technical interview, I faced a similar question. One solution that was used in production was changing the request type from POST to GET after the initial click. It can help manage how the calls are made.
A simple way to handle this is to add a loading state to your button. Disable the button once it's clicked, and keep it disabled until the request finishes. This way, you stop the spam clicks without needing complex logic.
You could also modify your backend to ignore repeated requests from the same user or session within a certain timeframe. This way, even if the button gets clicked multiple times, only the first request will be processed.
Check out the concept of 'debouncing'—it's a great technique to limit how often a function gets called. Basically, it allows you to set a delay between requests, so rapid clicks don't overwhelm your API.
Consider implementing throttling on your backend. There's a good resource here on throttling patterns: [Throttling Patterns](https://learn.microsoft.com/en-us/azure/architecture/patterns/throttling). It can help manage how many requests a user can make in a given time.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically