2
I have the following problem:
In a form, I need to select a MANUFACTURER, and according to this selection, it shows the PRODUCTS linked to that manufacturer only.
I have the following table where I register the MANUFACTURER:
FABRICANTES:
id_fabricante | nome_fabricante
1 | CANON
And I have the CAMERAS table, where I link the MANUFACTURER to the camera model
CAMERAS:
id_camera | fabricantes_id_fabricante (chave estrangeira) | modelo_camera
1 | CANON | 5D MARK II
Now that the tricky part comes in, I created the form
where it pulls PHP into the database the registered manufacturers:
<?php
$fabricantes = listaFabricantes($conexao);
?>
...
<tr>
<td>Fabricante:</td>
<td>
<select name="fabricantes_id_fabricante" id="fabricantes_id_fabricante" class="form-control">
<?php
foreach ($fabricantes as $fabricante):
?>
<option value="<?= $fabricante['id_fabricante'] ?>">
<?= $fabricante['nome_fabricante'] ?></br></option>
<?php
endforeach;
?>
</td>
</tr>
...
And then I created the form that lists the cameras:
<tr>
<td>Linha de Câmera:</td>
<td>
<select name="cameras_linhas_id_camera_linha" id="cameras_linhas_id_camera_linha" class="form-control">
<?php foreach($cameras_linhas as $camera_linha) :?>
<option value="<?=$camera_linha['id_camera_linha']?>">
<?=$camera_linha['nome_linha_camera']?></br></option>
<?php endforeach?>
</td>
</tr>
Only what I want to do now is when I select the CANON manufacturer it only appears the cameras that have relationship with that manufacturer, and not that list all the cameras.
The closest I could do was using Jquery as follows:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<script>
$('#fabricantes_id_fabricante').change(function(){
selection = $(this).val();
switch(selection)
{
case '1':
$('#cameras_linhas_id_camera_linha').show();
break;
default:
$('#cameras_linhas_id_camera_linha').hide();
break;
}
});
</script>
But in this case above, it only works if the SELECTS options are created in the html form and not fetching from the database to link.
I know it’s a little long, but could someone help with an idea of how you could do this Jquery query in the bd to return the results or some other light?
It would be something like what they do as State versus City, where they click on a given state and only the respective cities appear, and this information is fetched from a database. I’ve been racking my brain for a few days now, so I came here.
Thanks in advance.
To simplify can you put the HTML you have when the page loads? you already have both select with all options, or one of the select should change the options depending on the first?
– Sergio
I will post an example that I have using Ajax, should help you.
– Leonardo
I answered a question with the same question: http://answall.com/questions/162062/validates%C3%A7%C3%a3o-de-dois-selects/162066#162066. The only difference is that in
change
ajax query in PHP to get the list of cameras and then create the secondselect
.– BrTkCa
@Sergio what I would like is for you to list the SELECT1 information and according to this select, the SELECT2 options appear. Like it’s a country-state relationship. I have the countries Brazil and Portugal in SELECT1, when I select Brazil, appears SP, RJ, etc. But I would like all this information to come from the database, without me having to create the options in the form.
– Rogério de Sá Jr.
Hi. Welcome to SOPT. Do not put "SOLVED" in the title, and create yourself an answer with your solution (instead of editing and putting it in the body of the question). This site is not a forum. If you haven’t done it yet, do the [tour] and read to [help], ok? :)
– Luiz Vieira
Do not put the answer within the question. Confuses and disturbs...
– ShutUpMagda
Ready, I put as an answer. Thanks for all your help!
– Rogério de Sá Jr.
Not at all! : ) You also don’t have to thank us in the body of the answers. The best way to thank someone who helped you is by voting for their answers. And also consider accepting an answer to indicate to future readers who helped you the most or actually solved your problem. Good luck!
– Luiz Vieira