Multiple forms and one page processing

please i need your help with the problem. There are two forms on my homepage that I would like users to fill out and submit at different times. My problem is that I like to have only one processing page for both of them. I can usually do this on separate pages. But I would like to know if it is possible to do this on one page.

Good. If I submit Form A, the action page will not have a Undefined Index for a variable of Form B that has not been submitted, and, of course, using GET is not recommended.

Thanks for your time and patience.

+3
source share
3 answers

. , /submit.php?action=register /submit.php?action=activate.

, :

if ($_GET['action'] == 'register') {
  // Register user
} else if($_GET['action'] == 'activate' {
  // Activate user
}

:

if (isset($_POST['submit'])) {
  if ($_POST['submit'] == 'register') {
    // Register user
  } else if($_POST['submit'] == 'activate') {
    // Activate user
  }
}
+8

form_process script, .

if(!empty($_POST)){
 include 'form_process.php';
}

form_process.php / .

URL- , .

<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");
}
+3

. action .

You need to make some conditions and write individual functionality for Form Aand for Form Bdepending on the original form.

You can check using parameters in action like @Ami.

/submit.php?action=register or /submit.php?action=activate

So you have this code:

if ($_GET['action'] == 'register') {
  // Register user
} else if($_GET['action'] == 'activate' {
  // Activate user
}

However, you can 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
  }
}
0
source

All Articles