Action of the form for the same page or different page?

1

When creating an HTML form with insertion into the database, you can point the action of the form to the page itself or to another PHP script. I am in doubt about which would be the best option.

I always use a second page, and I make the second page validation too, if the password is wrong (for example, if it is a login form), I display a warning and redirect to the previous page.

Would it be advantageous for me to do it any other way? What are the differences between the two options?

    
asked by anonymous 03.06.2014 / 02:44

2 answers

4

There is no "good practice", "right" or "wrong". Use as you see fit. If you submit to the same page, you need to redirect if you pass the validation; if you submit to another page, you need to save validation errors in the section, and redirect if it does not.

What is very common is having a single PHP file as an entry point (or front controller ) of the entire application / site - usually a index.php . In this case, the forms are usually submitted to itself. Perhaps this gives the impression that this is the "correct".

    
03.06.2014 / 03:46
2

On "right" or "wrong", it really is something that does not exist in this context. However, it is a good practice to separate PHP from HTML, so you are usually submitting to another page where you can validate the data. This will make it easier for a future modification that another programmer will make, or even you.

But each case is a case, if you are working with a form only and this is not too big, throw everything in the same file and solve everything in it. Now if it is a system with several modules goes through the 1st tip, separate HTML from PHP.

    
05.06.2014 / 01:43