Form action for same page or different page?

Asked

Viewed 1,230 times

1

In the creation of an HTML form with insertion in the database, the action from the form to the page itself or to another PHP script. I’m in doubt about which would be the best option.

I always use a second page, and do the validation on the second page as well, 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 otherwise? What are the differences between the two options?

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 validation; if you submit to another page, you need to save the validation errors in the section, and redirect if you do not pass.

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

2

About "right" or "wrong", it really is something that does not exist in this context. However, it is good practice to separate PHP from HTML, so it is usually by submitting to another page, where you can validate the data. This will facilitate 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 one is not very large, plays everything in the same file and solves everything in itself. Now if it is a system with multiple modules goes by the 1st tip, separate HTML from PHP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.