How to make a dynamic select?

Asked

Viewed 927 times

0

inserir a descrição da imagem aqui

I’m doing a restaurant sisteminha, and he’s a commando. But, like, there’s a select of food orders, but I was wondering how I can do for when I click on the + sign appear another select under what already has to order other food, because not always the customer wants only one thing.

I’m doing the system with PHP and Javascript.

  • Don’t have any test code? Either this with javascript or it can be with jquery?

  • An idea would be a button (+) that when clicked, creates an html object of type DIV with an id and an incrementable name and inside that div another select with the same options. At the time of submitting the form, It will be necessary to see if the customer chose in different selects the same product and treat the repetition or, in the creation of the div, already put the previous select disabled and remove from the list of options of the new select created the option selected earlier. This will make a good workforce!

1 answer

4


Would that be it? Instead of selecting another select, I made an example that when selecting adds an input with the added value.

$(".add").on('click',function(){
var cont=0;
$("#selecionados input").each(function(){
if($(this).val()==$("#selecionar option:selected").html()){
cont++;
 }
});
if(cont>0){ alert("Este item ja esta adicionado, altere a quantidade se deseja mais..");}
else{
$("#selecionados").append("<input disabled type='text' name='pedidos[]' value='"+$("#selecionar option:selected").html()+"' ><input type='text' name='quantidade[]' placeholder='quantidade'><br>");
}
});
.add{ text-decoration:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selecionar">
<option selected isabled>Selecione</option>
<option>Pao</option>
<option>Leite</option>
<option>Cafe</option>
</select>
<a class="add" href="#">+</a>
<hr>
Selecionados
<hr>
<div id="selecionados">
  
</div>

And in php Voce it would take the value in array, like this:

<?php $valores=$_POST['pedidos'] 
foreach($valores as $item){
echo $item."<br>";
}


?>
  • Yes even so it became top. thanks. But and how to show the quantities that was requested from each ?

  • You can either serve php, or add input to the side, another input with quantity

  • @allanaraujo So? I edited there If solved your question, mark as answer :)

  • Thanks was great.

  • @allanaraujo Of anything, I put the check if the item is already added too...

  • But tell me one thing when I send the information to the database I have to make only one column for ordering food or several for each item added ?

  • @allanaraujo I recommend you to make two tables, an order and another item_request, on request, Voce stores the value, date, client and such. On request, Voce stores the order id, and each item added.

  • I will use this system only on localhost , I put everything else it does not work. Only works here on the site more on localhost it adds nothing . what could do

  • @allanaraujo open a new question and specify everything to be able to help you better

  • I already asked the question

  • How could I do this query ? http://answall.com/questions/178382/store%C3%Baltiplos-select-numa-coluna-s%C3%B3-com-php-javascript-mysql/

Show 6 more comments

Browser other questions tagged

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