Reputation: 4534
This is probably a stupid question but will ask anyway sine I have no idea.
I have written basic php code which serve forms. Say I have a login page and I serve it using the login.php page and it will be called in the login.html page like this -
<form action="login.php" method="post">
By this it is also implied that every POST needs its own php file, doesn't it? This kind of feels weird. Is there a way to have a single file, say code.php, and just have each of the forms as functions instead?
EDIT: Specifically, say I have 5 forms that are used one after the other in my application. Say after login the user does A, B, C and D tasks each of which are sent to the server as a POST request. So instead of having A.php, B.php, C.php and D.php I would like to have a single code.php and have A(), B(), C() and D() as functions. Is there a way to do this?
Also on the same note, how do I deal with say a global array (e.g. an array of currently logged in users) across multiple forms? I want to do this without writing to a DB. I know its probably better to write to a DB and query but is it even possible to do it with a global array? The reason I was thinking about having all the form functions in one file is to use a global array.
Thanks, - Pav
Upvotes: 0
Views: 759
Reputation: 5662
There is no reason why you can't just have login.html and login.php as the same file. Combine the code on both of these pages into one block of code and then use an if
statement to check whether the form has been posted. This was you can either serve the form to login or present the result of a successfully completed form.
A Session global variable will handle your global array problem but I don't think it's appropriate for your example. That example would need a database table to store when a user logs in as there is no other way to update the global variables on your instance of the application from someone else's instance.
EDIT
I would use a Session variable to determine how much of the form has been completed and then maybe use a switch statement to serve the correct form:
function a(){
//process form A
//if form A okay
$_SESSION['form_progress'] = 'b';
}
function b(){
//process form B
//if form B okay
$_SESSION['form_progress'] = 'c';
}
function c(){
//process form C
//if form C okay
$_SESSION['form_progress'] = 'd';
}
switch($_SESSION['form_progress']){
case 'b':
//serve form B
break;
case 'c':
//serve form C
break;
case default:
//serve form A
break;
}
Hope this helps
Upvotes: 0
Reputation: 522081
Sure, no problem, just POST all forms to the same .php file and have it deal with all incoming POST data. There's nothing keeping you from doing it.
Usually your forms send different data though and each form needs a bit of custom handling. If you put all this logic into the same handler file, it'll become a reaaaally long, messy piece of code. It's better to separate code into different files if it does different things.
You can and should abstract the common parts like database connection handling into functions or objects and include
them in each POST handler though.
Upvotes: 0