Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Three forms actions in one php page

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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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>
<? } ?>
about 4 years ago · Juan Pablo Isaza Relatório

0

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>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda