With pure Javascript you can do it this way:
php form.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function listarProdutos(categoria){
$.ajax({
url: 'listar.php',
data: {'categoria':categoria},
success: function(data) {
$('#lista').html(data);
}
});
}
</script>
</head>
<body>
<form>
<select id="categoria" name="categoria" onclick="listarProdutos(this.value);">
<option value="1">JOGOS</option>
<option value="2">ELETRONICOS</option>
<option value="3">ROUPAS</option>
</select>
Produtos:<br>
<div id="lista"></div>
</form>
</body>
</html>
When the combo option is changed it will call the file (example listar.php
), that takes user’s 'input' query in the bank (which replaced by arrays, $games, $electronics and $clothes) and creates the checkboxes the HTML output is returned by AJAX and played in div
list. This example is simple recommend that if possible remove the echo <input...
by template, an engine suggestion would be Smarty
<?php
function imprimirCheckbox($arr){
foreach($arr as $item){
echo '<input type="checkbox" name="produtos[]" value="'.$item.'" />'. $item .'<br>';
}
}
$jogos = array('COD', 'FIFA 2014', 'NFS');
$roupas = array('CAMISA', 'CALÇA', 'BERMUDA');
$eletronicos = array('PC','NOTEBOOK','TABLET');
if($_GET['categoria'] == 1){
imprimirCheckbox($jogos);
}else if($_GET['categoria'] == 2){
imprimirCheckbox($eletronicos);
}else{
imprimirCheckbox($roupas);
}
I’ll leave this for someone else to answer, but I tell you that you can research on AJAX. And avoid using
echo
, the code gets a little dirty... you can at any time close and reopen the PHP tag (<?php ?>
), and everything outside of it is text as inecho
-- just don’t need to escape quotes.– André Leria