I am junior PHP dev and I am currently working on making a quote form which will take customer details and whatever the employee enters in the quote form and will:
- Save customer and quote detail to DB.
- Generate a PDF (using mpdf library)
- Email the pdf to the customer
Currently its just two files index.php and quote.php
Here is a diagram
As you can see it is a monolithic architecture (I have all the operation in one PHP file).
This is causing problems as when the user hits “SUBMIT” and quote.php does all its functions , the user is stuck on that page. Then if they hit Refresh , quote.php will be re-executed again and it ends up spamming my database tables, and email inbox.
I was wondering what is the best way to prevent the user from re-executing or going back to doing stuff which should only be done once.
I had a couple of ideas and was hoping you could suggest me yours?
-
Idea 1: A session variable that prevents re-execution of the same code.
-
Idea 2: Break up the quote.php file into seperate files and jump from each one. (is this possible)?
I want to do this the best way possible and I am open to any suggestions!
Thankyou for your time!
🙂
If the form takes a while to process (PDF generation can take time) then a good first step would be to stop the user from re-submitting the form. Use javascript to disable the submit button and maybe show a loading icon so they know something is happening.
Once the code has finished executing you will want to redirect them to another page so that you don’t have the problem of them refreshing the page.
This is quite simple, just send a header…