Difficulties with AJAX JavaScript PHP

Started by vizzmedia, Oct 19, 2022, 12:51 AM

Previous topic - Next topic

vizzmediaTopic starter

Please tell me how to deal with such a situation. I have a registration form in the markup of the page, when you click on the button, the entry file in the database begins to act. In JavaScript, the event handler checks the fields for validity and if there is a filling error, the action of sending the form on the markup with the command e.rpeventDefault is canceled.
There are no problems in this part, but problems appear when I add an ajax request to one of these checks using the GET method to verify the login with the database. This ajax is also called in the event handler click on the registration button.
In theory, I wanted to make it so that if such a login already exists, the default execution of pressing the button is interrupted, that is, e.preventDefault, but the problem is that the program first calls ajax, Ajax executes the php script, and while it is running, the js script has long gone into the sunset and finished its a job.

I made ajax synchronous, but it didn't help. You need to do something so that JavaScript waits for php to finish and then continues working, or immediately disable the execution of the default button click and then do something so that, upon successful verification from the database, a function is called that sends the form to PHP and an entry to the database by means of JavaScript. How can I solve my problem in such a situation?




Apologize for the directness, don't kick hard.
But for me, ajax needs to be used to communicate with the database, i.e. the server part should only be able to select, update, insert and delete data from the database. If you implement all these functions correctly, then the load on the server will be minimal.

As for the graphs, it seems to me that it is not rational to overdo all the graphs at once when changing. If the condition is that, for example, 10 people work with data at the same time, it turns out that every time a cell is saved, the graph is rebuilt... An accident that may never occur is that all 10 clients will simultaneously click save. Let's simplify everything and assume that the request and processing will take 1 second, i.e. in our case it turns out that the server will rebuild the schedule 10 times. Question and for what?
When you can just run the graph generation script once in the same second, it turns out that the server will not do the same job 10 times, but will do it once.
This is my personal opinion which I adhere to. That's how I write websites. The server works with the database, the client part does the rest.