I have two search forms. The first with the name "search" the second "searchimv" needs a php code that checks if data is being received from one of the two only, otherwise it will be redirected to a specific page.
I have two search forms. The first with the name "search" the second "searchimv" needs a php code that checks if data is being received from one of the two only, otherwise it will be redirected to a specific page.
A simple way to do this, only with php / html is to define a hidden one that identifies which form to trigger, in this example I imagine that both use the same action. You can also 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'];
}
In addition to the appropriate 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, you would not even need this because, as there are fields of the name, you can test them directly: isset( $_POST['cpf'] )
An alternative that works perfectly even though it is not exactly within specs , is to add a GET parameter in 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, notice the action passing the "type" by query string ?tipo=