Home > Enterprise >  Three forms actions in one php page
Three forms actions in one php page

Time:03-06

Assume that I have a PHP page that has three forms.

  1. Form one, it will ask the user to enter the email address while the code is hidden.
<label for="email">Email Address:</label> <input type="text" id="email" name="email">
<input type="hidden" id="code" name="code" value="42"> <input type="submit" value="Submit1">
  1. Form two will be showing after step 1. In this step, the user will be asked to enter the code in step 1 (which is equal to 42).
<input type="number" id="code2" name="code2"> <input type="submit" value="Submit2">
  1. Form three, will be showing after entering corrected code in step 2.
<input type="number" id="code2" name="code2"> <input type="submit" value="Submit3">

How can I program it?

CodePudding user response:

You could try this approach :

<?php 
function getFormContent() {
  $email = $_POST['email'] ?? null; //$email equals to null if it is undefined, to prevent errors

  if (!$email) {
    echo '<input type="text" name="email" placeholder="Email.." />';
    echo '<input type="hidden" id="code" name="code" value="42">';
  } else {
    echo '<input type="hidden" name="email" value="'.$email.'" />'; //keep email set after submitting first step
  }

  if ($email && $_POST['code']) {
    if (isset($_POST['code2']) && $_POST['code2'] == $_POST['code']) {
      //final form
    } 
    else { //second step
      echo '<input type="number" id="code2" name="code2">';
      echo '<input type="hidden" id="code" name="code" value="42">'; //we still need this in case user submitted a wrong code
    }
  }
}
?>

<form method="post">
  <?= getFormContent() ?>
  <input type="submit" />
</form>

CodePudding user response:

You need to state the action of the form as the name of the file. So, if the file you are working on is name example.php it should look like so:

$email = trim(get_variable_value('email'));
$code = trim(get_variable_value('code'));
$code2 = trim(get_variable_value('code2'));

//make sure to validate the input!! if it's not valid send back

<? if(!isset($email)){ //alternatively, you can add a hidden input `stage`. I just don't like it, as it can be bypassed.
?>
    <form name="transferToNewForm" action="example.php" method="post">
        <label for="email">Email Address:</label> <input type="text" id="email" name="email">
        <input type="hidden" id="code" name="code" value="42">
        <input type="submit" value="Submit1">
    </form>
<? } 
if(isset($email) && isset($code) && $code == 42){ ?>
    <form name="transferToNewerForm" action="example.php" method="post">
        <label for="email">Email Address:</label> <input type="text" id="email" name="email" value="<?= $email ?>" readonly>
        <input type="number" id="code2" name="code2">
        <input type="submit" value="Submit2">
    </form>
<? } 
if(isset($email) && isset($code2) && $code2 == 42){ ?>
    <form name="finalFrom" action="theActualPageYouWant.php" method="post">
        <label for="email">Email Address:</label> <input type="text" id="email" name="email" value="<?= $email ?>" readonly>
        <input type="number" id="code2" name="code2">
        <input type="submit" value="Submit3">
    </form>
<? } ?>
  • Related