2
I have two search forms. The first with the name "buscaid" the second "buscaimv" needs a php code that checks whether data is being received from either of the two only, otherwise it will be redirected to a specific page.
2
I have two search forms. The first with the name "buscaid" the second "buscaimv" needs a php code that checks whether data is being received from either of the two only, otherwise it will be redirected to a specific page.
3
One simple way to do this, only with php/html is to define a Hidden that identifies which form is triggered, in this example I imagine that both use the same action. It is also possible to do this with javascript.
Example - simple.
<form method="post" action="">
<input type="hidden" name="tipo" value="pf">
CPF: <input type="text" name="cpf" value="111.111.111.11" />
<input type="submit" />
</form>
<form method="post" action="">
<input type="hidden" name="tipo" value="pj">
CNPJ: <input type="text" name="cnpj" value="222.222.222.222/22" />
<input type="submit" />
</form>
<?php
if($_POST['tipo'] == 'pf'){
echo $_POST['tipo'] .'-'. $_POST['cpf'];
}else if($_POST['tipo'] == 'pj'){
echo $_POST['tipo'] .'-'. $_POST['cnpj'];
}
3
In addition to the proper solution posted by @rray, you can use the Submit fields to differentiate the Forms:
<form method="post" action="">
CPF: <input type="text" name="cpf" value="111.111.111.11" />
<input name="enviar_pf" type="submit" value="Enviar"/>
</form>
<form method="post" action="">
CNPJ: <input type="text" name="cnpj" value="222.222.222.222/22" />
<input name="enviar_pj" type="submit" value="Enviar"/>
</form>
<?php
if( isset( $_POST['enviar_pf'] ) ){
echo 'CPF: '. $_POST['cpf'];
} elseif( isset( $_POST['enviar_pj'] ) ) {
echo 'CNPJ: '. $_POST['cnpj'];
}
Note that in the case you posted, even this would not need to, because there are right name fields, you can test directly by them: isset( $_POST['cpf'] )
An alternative that works perfectly despite not being exactly within the specs, is to add a GET parameter to the POST:
<form method="post" action="?tipo=cpf">
CPF: <input type="text" name="cpf" value="111.111.111.11" />
<input name="enviar_pf" type="submit" value="Enviar"/>
</form>
<form method="post" action="?tipo=cnpj">
CNPJ: <input type="text" name="cnpj" value="222.222.222.222/22" />
<input name="enviar_pj" type="submit" value="Enviar"/>
</form>
<?php
if( $_GET['tipo'] == 'cpf' ){
echo 'CPF: '. $_POST['cpf'];
} elseif( $_GET['tipo'] == 'cnpj' ) {
echo 'CNPJ: '. $_POST['cnpj'];
}
In this case, note the action passing the "type" by query string ?tipo=
Browser other questions tagged php form script
You are not signed in. Login or sign up in order to post.
look at the php.net manual: there talks a little more about the use of variables and about the method:
$_POST
– Ivan Ferrer