3
Good afternoon!
I have a database with two tables, products and menus, the two tables are indexed through the Cdcardapio field. I created an html select element that contains all the registered menus, I need that when selected, for example, the menu "Drinks", only the products that Cdcardapio is equal to the selected are listed. However, I need this to happen without updating the page, here’s the problem, I haven’t learned to work very well with jQuery and Ajax, someone can help me?
I already have the code that lists the existing menus:
<select class="form-control" id="cardapio" name="CdCardapio">
<option value=""> Selecione um Cardápio </option>
<?php foreach($cardapios as $cardapio) : ?>
<option class="cardapio" value="<?=$cardapio['CdCardapio']?>">
<?=$cardapio['nomeCardapio']?>
</option>
<?php endforeach ?>
</select>
Now I need that according to the selected menu, the products belonging to it are listed in the table below, which, now, I am listing all products, see:
<table class="table table-bordered">
<tr class="active">
<td>
<b>Produto</b>
</td>
<td>
<b>Valor</b>
</td>
<td>
<b>Quantidade</b>
</td>
</tr>
<?php
$produtos = listaProdutos($conexao);
foreach($produtos as $produto) :
?>
<tr class="produto">
<td hidden><?=$produto['CdProduto'] ?></td>
<td hidden><?=$produto['CdCardapio'] ?></td>
<td><?=$produto['nomeProduto'] ?></td>
<td><div class="valor"><?=$produto['valorProduto']?></div></td>
<td>
<input class="quantidade" type="number" value="0" size="1" maxlength="2" max="10" min="0" step="0">
</td>
</tr>
<?php
endforeach;
?>
</table>
Function listProducts:
function listaProdutos($conexao) {
$produtos = array ();
$resultado = mysqli_query($conexao, "select p.*, c.nomeCardapio
from cardapios c, produtos p
where c.CdCardapio = p.CdCardapio");
while($produto = mysqli_fetch_assoc($resultado)) {
array_push($produtos, $produto);
}
return $produtos;
}
I think it’s clear now, sorry for the inconvenience!!
Have you tried anything? If so, post your code with the specific problem. If we wouldn’t have to create the whole routine from scratch.
– DontVoteMeDown
The posted solution gives you to adapt, Status => Menu / City => Products...
– Sr. André Baill
But why don’t you adjust it like I showed you?
– Sr. André Baill
When you posted I was editing my question! I will adjust it as soon as it is ready I put it here. Thanks André!
– Fred
Okay, anything if you can’t, post what you can adjust and we’ll analyze it here and help you finish. Thanks! ;)
– Sr. André Baill
Okay, I have a question, in my case I need to show the data in a table, not in a select, to be specific are two database fields that I need to load in this table. To click on select, I did some examples here and it worked, but I don’t know how I could do it.
– Fred
You can do it too, you got it right?
– Sr. André Baill