0

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?

2
  • 1
    You can put the form action as the current URL, and add ifs to the other forms, based on the stage the user is on. Is that a clear enough explanation? Commented Mar 6, 2022 at 9:40
  • If you can please support me with the code as I struggle with it. Appreciate your assistance. Commented Mar 6, 2022 at 9:45

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

very nice. In case I don't want to show <input type="submit" /> in the final form, how can I do it?
You could set a variable (e.g. $finalForm = true) in the final form condition, and wrap the button submit in a if (!isset($finalForm)) condition
Would it be possible to update it on the code please. Sorry for any incontinence caused you.
1

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>
<? } ?>

5 Comments

( ! ) Parse error: syntax error, unexpected ';', expecting ')' in C:\wamp64\www\form2\index.php on line 4. This is in line 4 $email = trim(get_variable_value('email');
fixed. I was missing a ) on the first 3 lines @HishamAlghamdi
sorry, got this error: ( ! ) Parse error: syntax error, unexpected '<' in C:\wamp64\www\form2\index.php on line 11 On this line: <form name="transferToNewForm" action="example.php" method="post">
right, I didn't include the <?php ?> tags. I'll edit in a moment @HishamAlghamdi
I still get errors: Notice: Undefined variable: email in C:\wamp64\www\f2\index.php on line 18 Call Stack #TimeMemoryFunctionLocation 10.0022238936{main}( )...\index.php:0 " readonly> and Notice: Undefined variable: email in C:\wamp64\www\f2\index.php on line 25 Call Stack #TimeMemoryFunctionLocation 10.0022238936{main}( )...\index.php:0 " readonly>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.