How do I get the value of a specific form from the previous page in PHP?

Asked

Viewed 101 times

0

Good afternoon!

A PHP page contains two forms form, each with a submit. How to obtain data from form 1, for example, after pressing the Submit through the POST? Usually when there was only one form it would pick up the data directly through the code below: (CNPJ being the id of a input form):

table {
  float: left;
}
<form id="localizacao" name="localizacao" method="post" action="rastreamento.php" onsubmit=" return false;">
  <table width="50%" border="1">
    <tr>
      <div align="center"><img src=img/logo.jpg></div>
    </tr>
    <tr>
      <th colspan="5" align="center" valign="top">
        <h2>Pesquisa</h2>
      </th>
    </tr>
    <tr>
      <td width="156">Selecione o STATUS:</td>
      <td>
        <select name="status" id="status">
          <option value="0">Recebido</option>
          <option value="1">Em trânsito</option>
          <option value="2">Encerrado</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>CNPJ:</td>
      <td width="835"><input name="CNPJ" type="text" id="CNPJ" size="20" maxlength="14" />
        <span class="style1">*</span> <span class="style3">somente n&uacute;meros</span></td>
    </tr>
    <tr>
      <td>
        VIAGEM:
      </td>
      <td width="835"><input name="VIAGEM" type="text" id="VIAGEM" size="20" maxlength="14" />
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <p>
          <input name="pesquisar" type="submit" id="pesquisar" value="Pesquisar" />
          <br />
          <input name="limpar" type="reset" id="limpar" value="Limpar!" />
          <br />
        </p>
      </td>
    </tr>
  </table>
</form>
<form id="alteracoes" name="alteracoes" method="post" action="rastreamento.php" onsubmit="return validaCampoAltera(); return false;">
  <table width="50%" border="1">
    <tr>
      <th colspan="5" align="center" valign="top">
        <h2>Modifica</h2>
      </th>
    </tr>
    <tr>
      <td width="50">VIAGEM:</td>
      <td width="835"><input name="VIAGEM" type="text" id="VIAGEM" size="20" maxlength="14" />
    </tr>
    <tr>
      <td width="50">NOVO STATUS:</td>
      <td width="835"><input name="NVSTATUS" type="text" id="NVSTATUS" size="1" maxlength="1" />
    </tr>
    <tr>
      <td width="50">EMBARCACAO:</td>
      <td width="835"><input name="EMBARC" type="text" id="EMBARC" size="20" maxlength="10" />
    </tr>
    <tr>
      <td width="50">DATA SAIDA:</td>
      <td width="835"><input name="DATSAI" type="text" id="DATSAI" size="20" maxlength="10" />
    </tr>
    <tr>
      <td width="50">HORA SAIDA:</td>
      <td width="835"><input name="HORSAI" type="text" id="HORSAI" size="20" maxlength="10" />
    </tr>
    <tr>
      <td width="50">DATA CHEGADA:</td>
      <td width="835"><input name="DATCH" type="text" id="DATCH" size="20" maxlength="10" />
    </tr>
    <tr>
      <td width="50">HORA CHEGADA:</td>
      <td width="835"><input name="HORCHE" type="text" id="HORCHE" size="20" maxlength="10" />
    </tr>
    <tr>
      <td colspan="2">
        <p>
          <input name="atualizar" type="submit" id="atualizar" value="ATUALIZAR" />
          <br />
        </p>
      </td>
    </tr>
  </table>
</form>

if (isset($_POST["CNPJ"])){ $CNPJ = $_POST ["CNPJ"]; }

  • Please include more information and detail better

  • If they are two forms, they will be submitted individually: either submit the first, or the second. Submit one does not send the data of the other.

  • @Andersoncarloswoss, how to get him to take the data from formulario1, for example? It would be something like $CNPJ = $_POST ["formulario1.CNPJ"]?

  • What is your HTML code like? What form are you submitting?

  • @Andersoncarloswoss, question edited with HTML code.

  • 1

    @Marvin you will only have access to the data of the submitted form, if you do not submit the first you will not have access to his data.

  • You could only use a button to submit both forms at the same time, otherwise I think there is no form.

Show 2 more comments

2 answers

0

Each page form, regardless of how many are submitted, is submitted individually. So, if you have two forms and each one has your Submit, only the form in which Submit was triggered will have the data sent to the server, remembering that each form should have its Submit. In your case, if you click on the "location" form Ubmit, only its inputs will be sent via post, even if there are inputs in the other form with the same name. This should also be handled in your back-end, since both forms send to the same file, your code should know how to compare, filter and treat the capture of the data according to the source form of the same.

0


The solution was to take away the space that was in onsubmit and put true.

onsubmit=" return false;"

for

onsubmit="return true;"

in

<form id="localizacao" name="localizacao" method="post" action="rastreamento.php" onsubmit="return true;">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.