Multiple forms and one processing page
please i need your help with an issue. I have two forms on my homepage that i'll like users to fill and submit at different times. My problem is that i'll like to have only one processing page for both of them. Normally i can do this in seperate pages. But i'll like to know if doing it on the same page is possible.
Okay.. If i submit form A, on the action page, wont there be Undefined Index for variable of form B, that has not being submitted, and ofcourse using a GET is not adviced.
Thanks for your time and patience.
It's not completely unheard of to do this. Quite often, a different parameter is passed in the form element's action attribute like /submit.php?action=register
or /submit.php?action=activate
.
So, you have code like this:
if ($_GET['action'] == 'register') {
// Register user
} else if($_GET['action'] == 'activate' {
// Activate user
}
However, you could also just change the value of the submit button and have the action attribute the same for both forms:
if (isset($_POST['submit'])) {
if ($_POST['submit'] == 'register') {
// Register user
} else if($_POST['submit'] == 'activate') {
// Activate user
}
}
create separate form_process script then include in form pages.
if(!empty($_POST)){
include 'form_process.php';
}
form_process.php should contain only class/function without echo or print out.
alternately you may set action url to the same page then redirect back to proper page.
<form id="add-profile-form" action="form_controller.php" method="post">
<input type="hidden" name="act" value="adding"/>
<!-- form 1. -->
</form>
<form id="edit-profile-form" action="form_controller.php">
<input type="hidden" name="act" value="editing"/>
<!-- form 2. -->
</form>
form_controller.php
if(isset($_POST['act']){
if($_POST['act'] == 'adding'){
//process form1
}else if($_POST['act'] == 'editing'){
//process form2
}
header("Location: success.php");
}
You can do it on the same page also. Just you need to make action
same for both the forms.
You need to make some condition and write the individual functionality for Form A
and for Form B
depending on the source form.
You can check with the parameters in action like @Ami has used.
/submit.php?action=register
or /submit.php?action=activate
So, you have code like this:
if ($_GET['action'] == 'register') {
// Register user
} else if($_GET['action'] == 'activate' {
// Activate user
}
However, you could also just change the value of the submit button and have the action parameter the same for both forms:
if (isset($_POST['submit'])) {
if ($_POST['submit'] == 'register') {
// Register user
} else if($_POST['submit'] == 'activate') {
// Activate user
}
}