1 input and a select to 1 same variable

1

Good evening, mates

I am making a form to request a code or a name, so that I go and search in a database. To make it easier for the user, I would like to give you the option to write them down or look for them in a drop-down list directly brought from the DB data My question is.

Is it possible that having the entry fields without the need to make 2 forms? The idea is that whoever receives only the one that contains a value, but passes the 2

I add the code

<form method='get' action='consulta.php'>
        	<input name="equipo" type="text" />
     		<select name="equipo">
            <option selected="selected"></option> 	
	<?php while($array= mysql_fetch_array($consulta)){?> 	
	<option value="<?php echo $array['codigo']?>"> 		
<?php echo $array['codigo']." | ". $array['nombre']?></option> 			<?php }?> 		
</select>
        <input type='submit' value='Consultar'> 	
	</form> <?php } ?>
    
asked by Cesar Blandon 13.11.2016 в 05:48
source

4 answers

0

First, you can not duplicate the name attribute, since they reference the form data after a form is submitted.

  

Here is a small example in PHP :

<form method='POST' action='consulta.php'>
    <input name="equipo" type="text" />
    <select name="equipo_list">
        <option selected="selected" value="0">Selecciona un equipo</option>     

        <option value="equipo_1">       
            Equipo 1
        </option>   

        <option value="equipo_2">       
            Equipo 2
        </option>           

    </select>
    <input type='submit' name="consulta" value='Consultar'>     
</form>

Let's see how the page would be consulta.php , then you can extend it to your liking or enter AJAX to not reload the search.

<?php //Pagina consulta

    $reporte = $equipo = $equipo_list = NULL;

    if (isset($_POST['consulta'])) {
        //Comprobacion si algun campo este vacio.
        if ( empty($_POST['equipo']) && empty($_POST['equipo_list'][0]) ) {

            $reporte = "Error, Debes escribir o seleccionar un 'equipo'";

        } elseif ( !empty($_POST['equipo']) ) { //Obtenemos el dato con el input
            $equipo = $_POST['equipo'];
        } else { //Obtenemos el dato mediante el select
            $equipo_list =  $_POST['equipo_list'];
        }

        if ($equipo) {

            //Hacemos algo con $euipo
            echo "OK equipo";

        }

        if ($equipo_list) {

            //Hacemos algo con $equipo_list
            echo "OK equipo_list";

        }
    }

    //MSG errores.
    echo $reporte;

?> 
    
answered by 13.11.2016 / 16:05
source
1

Maybe you could do it with Javascript , first you would remove the attribute name from input and select , and you would add it using javascript when doing submit of the form.

Something like for example:

var myForm = document.getElementById('TeamForm');
var inputTeam = document.getElementById('InputTeam');
var selectTeam = document.getElementById('SelectTeam');

myForm.addEventListener('submit',function(){

    if(inputTeam.value != '')
    {
        inputTeam.setAttribute('name','team');
        selectTeam.removeAttribute('name');
        console.log(inputTeam);
        console.log(selectTeam);
    }
    else if(selectTeam.value != 0)
    {
          selectTeam.setAttribute('name','team');
          inputTeam.removeAttribute('name');
          console.log(inputTeam);
          console.log(selectTeam);
    }
    else
    {return false;}
      
    // Send to action
  
},false);
<form method="GET" id="TeamForm">

    <input id="InputTeam" type="text" />
    <select id="SelectTeam">
      <option value="0">Select a team</option>
      <option value="1">Team 1</option>
      <option value="2">Team 2</option>
    </select>
    <button type="submit">Send</button>
</form>

This would allow you to send only one element with the attribute name , and at the end you would enter the code to send the data to your action in PHP . You could do it with AJAX .

If you check the console, here you will see that only one element is sent with the name attribute.

    
answered by 13.11.2016 в 15:20
0

Within the form, there can not be several fields with the same name. The input and the select must have different names because it is what php uses to identify one of the other when you make the GET request to the server you are using.

    
answered by 13.11.2016 в 06:26
0

Good afternoon I could already give a solution taking as reference the code that D Bulten sent me. What I did was validate where they were sending the data before sending it to the query and send only one

<form action="" method='post'>
    <input type='text' name="equipo" />
	<select name="equipo_list" >
            <option selected="selected"></option>
			<option value="equipo1">equipo1</option>
			<option value="equipo1">equipo2</option>
			</select>
		<input type='submit' value='Consultar' name="consultar"/>
</form>
<?php
if (isset($_POST['consultar'])) {
	//Comprobacion si algun campo este vacio.
        if ( empty($_POST['equipo']) and empty($_POST['equipo_list']) ) {
				 echo "Debe seleccionar uno de los campos";
			} elseif ( !empty($_POST['equipo']) ) { //Obtenemos el dato con el input
            $equipo = $_POST['equipo'];
			header('location: consulta.php?equipo='.$equipo);
        } else { //Obtenemos el dato mediante el select
            $equipo =  $_POST['equipo_list'];
			header('location: consulta.php?equipo='.$equipo);

        }
 }
 ?>
    
answered by 14.11.2016 в 17:39