I use forms a lot in my websites, so I was wandering what is better to submit the form the normal way of submit button or using ajax. I usually use ajax to prevent the form re-submission which may cause double insertion of some rows to the database which may cause problems, I know I could prevent the form re-submission if I chose to submit the form using submit button by using the preventDefault method on the form submission, but using the normal submit button will require me to include the php file that will deal with the form data in the same php file that contains the html of the form, which I consider not a practical way because this will make the user's browser read the code in the php file that will deal with the form data, even if the user did not submit the form that triggers this code, so I think it may cause slowness of loading for the website. So, what do you think?
Asked
Active
Viewed 61 times
-1
-
1*but using the normal submit button will require me to include the php file that will deal with the form data in the same php file that contains the htnl of the form* Can't this be done by setting the form's `action` attribute? Also, plain PHP code is not sent to the client, only stuff that the PHP echoes is – CertainPerformance Aug 08 '19 at 06:01
-
what you use depends on what the best solution is ... i.e. a hammer for a nail, a screwdriver for a screw - you can use a hammer on a screw or a screwdriver on a nail, but they are not the best tools for the job – Jaromanda X Aug 08 '19 at 06:01
-
`this will make the user's browser read the code in the php file` - nope, the browser has no access to the php file. What the browser gets is the output of PHP processing the php file – Jaromanda X Aug 08 '19 at 06:03
-
Related: https://ux.stackexchange.com/questions/57599/when-should-i-use-ajax-to-submit-forms-vs-regular-page-submit Also, ajax requires Javascript (but you're free to ignore the 1% of real users without JS enabled, if you wish) – CertainPerformance Aug 08 '19 at 06:04
-
the action attribute will redirect the user to the page in the value of the action attribute which I do not want specially that i would like to first check if the form inputs are all set before and display custom alerts (in case you think about using the "required" attribute ) sending data. @CertainPerformance – Youssef Ashour Aug 08 '19 at 06:06
-
You may use the `target` attribute of the form to indicate an iframe to submit to, without replacing the page: https://stackoverflow.com/questions/2866063/submit-form-without-page-reloading But that's a little bit convoluted. I think I'd use Ajax – CertainPerformance Aug 08 '19 at 06:07
1 Answers
0
1) Submitting forms is what they were created for. You can use ajax submission, when it is more convenient for users though. But it shouldn't be everywhere.
2) A separate php file shouldn't contain form code. You can determine a specific file or address with action="".
3) Browsers don't read the code in your php files. Servers do it and return only html data.
4) If you slow pages by using php forms, it means that your web-site engine is not designed correctly. Usually for users there is no difference between ajax and reload submiting.
Conclusion: It's normal to send requests to separate pages and most frameworks work like than. However, in some cases such as multiple forms on one page, you may need use ajax approach.

Max Shaian
- 418
- 4
- 11
-
`Servers do it and return only html data` or any other format, depending on the code in the PHP file :p – Jaromanda X Aug 08 '19 at 06:08
-
Absolutely. That's why I said usually. A lot of frameworks use this approach. But obviously, in some situations there is no need to reload the whole page depending on how much time it takes. – Max Shaian Aug 08 '19 at 06:13
-