Best way to process forms
Question by Gatura
What is the best way to process PHP forms, using $_SERVER['PHP_SELF']
or using a separate file for processing?
Answer by Starx
This is a very vague question. Answers depends on several factor
- Architecture
What architecture are you using, it is a basic PHP, or OOP, or MVC?
- Manaegiblity
How do you prefer to manage your coding? Do you prefer HTML and PHP coding mixed in a same page?
- Flexibility
How much flexibilty does your coding needs to be? Should it be able to increase and decrease form elements and its processing on Demand?
- Coupled
How much tightly/loosely coupled, your form and PHP codes are? Does the modules handling Form submit, work independent of form type?
Update
Create a seperate folder, where you will process the post of your page similar to this
mypage.php
process/mypage.php
mypage.php
will contain the form with action field as follows
<form action="<?php echo "process/".$_SERVER['PHP_SELF']; ?>">
By using the PHP_SELF
in this case will make the code reusable, with out making changes to it, on other forms also.
Now you can handle your POST request on process/mypage.php
and redirect to /mypage.php
, once it is done.