combo box and input text

0

It's the following I'm developing a form, I wanted one of the form fields to appear in an input text and select html, and if the person chooses the select item with several option's, was sent the data entered to the database with the option item that was selected with the input data; I in the form I am going to get the values by ID since they are 2 items, still working? How would I do that?

<select id="inserir_dados">
  <option>Exemplo 1</option>
  <option>Exemplo 2</option>
  <option>Exemplo 3</option>
  <option>Exemplo 4</option>
</select>
<input type="text" id="inserir_dados" placeholder="Caso nao exista"/>
    
asked by anonymous 22.12.2017 / 00:50

1 answer

1

First you should not use the same id in more than 1 element. This is wrong.

You should assign different names in the combo and the name , and put values ( input ) in each value of combo . It is these option that will be captured in PHP according to value :

<select id="inserir_dados1" name="meu_nome">
  <option value="1">Exemplo 1</option>
  <option value="2">Exemplo 2</option>
  <option value="3">Exemplo 3</option>
  <option value="4">Exemplo 4</option>
</select>
<input name="meu_nome2" type="text" id="inserir_dados2" placeholder="Caso nao exista"/>

When submitting the form, in PHP you will capture the combo and name value and set what will be sent to the bank according to the value of each one by the criteria you want (eg, if the combo value is not empty or input ):

<?php
    $meu_nome = $_POST['meu_nome']; // valor da combo
    $meu_nome2 = $_POST['meu_nome2']; // valor do input
?>

Let's say that if no input was sent some value and you want to ignore the combo value:

<?php
    $meu_nome = $_POST['meu_nome']; // valor da combo
    $meu_nome2 = $_POST['meu_nome2']; // valor do input

    if(!empty($meu_nome2)){
       $valor_que_vai_pro_banco = $meu_nome2;
    }else if(!empty($meu_nome)){
       $valor_que_vai_pro_banco = $meu_nome;
    }
?>
    
22.12.2017 / 01:36