combo box and input text

Asked

Viewed 827 times

0

It’s the following I’m developing a form, wanted that one of the fields of the form appears an input text and select html, and if the person chose the select item with several options, the entered data was sent to the database with the option item that was selected with the input data; I in the form I am going to fetch the values by ID since it is 2 items, it still works? 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"/>

  • 1

    Hello, Shider. Put more codes related to what you intend to do, because by the description itself I didn’t understand anything. Provide as much information as possible so your question is not negative.

1 answer

1


First you shouldn’t use the same id in more than 1 element. This is wrong.

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

<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"/>

By submitting the form, in PHP you will capture the value of combo and of input and define what will be sent to the bank according to the value of each one by the criterion you want (e.g. if the value of the combo is not empty or the input):

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

Let’s say that if in the input has been sent some value and you want to ignore the value of combo:

<?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;
    }
?>
  • DVD isset should not be in _POST? because if not it will only serve to check if it is NULL, instead of the key existing in superglobal.

  • @Guilhermenascimentop. I switched to empty... I only put a part to get the value. The validation of the form I imagine it should have.

  • yes I think that’s what I’ll try to practice!!

  • and yes I will fetch the values of inputs and so by POST method

  • @Shider glad I could help. Thank you!

  • @dvd Thank you I!

Show 1 more comment

Browser other questions tagged

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