Update a div with sql data without reloading the page

0

Hello everyone, I'm doing a site in php, in which there is a button section. When I select a button, it should do a search in the DB and with the result, update a div under the buttons, without reloading the page.

It is an e-commerce system, in which when selecting the color, it displays the sizes registered in that color.

What is the best way to do this?

The code below selects the color:

if($n->getNome() == "COR"){ ?>
                    <div class="row cor">
                        <div class="bloco_cor">
                            <div id="cor" class="texto">Cor</div>
                            <?php  

                                $pVariacao = new ProdutoVariacaoDao();

                                foreach ($produto as $prod) {
                                    $pv = $prod->getIdProdutoBase();
                                }

                                $teste = $pVariacao->findByIdAgrupado($pv);

                                foreach ($teste as $teste1) {
                                    $teste2 = $teste1->getIdOpcaoVariacao1();
                                //}


                                $opVariacao = new OpcaoVariacaoDao();
                                $opVar1 = $opVariacao->findById($teste2);
                                //var_dump($opVar1);

                                //foreach ($opVar1 as $op) {
                                    //$idOpVariacao1 = $op->getIdOpcaoVariacao();
                                    //echo "id var: ", $idOpVariacao1;

                                    $cores= $opVar1->getValor();
                                    $arrayCores = split("/", $cores, 2); ?>
                                    <div id="<?php echo $teste2 ?>" class="opcao_cor">
                                        <input class="inputIdProd" type="hidden" value="<?php echo $idProdutoBase; ?>">
                                        <div class="cor2" style="background-color:<?php echo $arrayCores[0];?>"></div>
                                        <div class="cor2" style="background-color:<?php echo $arrayCores[1];?>" ></div>
                                    </div>  
                            <?php } ?>      
                        </div>
                    </div>
        <?php } }?>

The following code selects the sizes registered in the DB for the color selected above:

<div id="tamanho" class="row tamanho">
                        <div class="bloco_tamanho">
                            <div class="texto">Tamanho</div>


                            <?php if (isset($_SESSION['tamanhos'])) {

                                $opVariacao2 = new OpcaoVariacaoDao();
                                $tamanhos = unserialize($_SESSION['tamanhos']); 

                                foreach ($tamanhos as $tam) { 
                                    $id = $tam->getIdOpcaoVariacao2();
                                    $estoque = $tam->getEstoque();


                                    $t = $opVariacao2->findById($id);

                                    if ($estoque == 0) { ?>
                                        <div class="tam tam_ausente" id="tam1"><span></span><?php echo $t->getValor(); ?></div>
                                    <?php } else { ?>
                                        <div class="tam" id="tam1"><span></span><?php echo $t->getValor(); ?></div>
                                    <?php } ?>

                                <?php } ?>


                            <?php } ?>



                        </div>
                    </div>
    
asked by anonymous 28.10.2015 / 17:49

1 answer

1

Use ajax:

$("select[name=cor]").change(function(){
    $.getJSON("linkdeconsulta",{idcor:$(this).val()},
        function(data){
            $.each(data, function(i, obj){
                $(".classdadivquevaireceberovalor").val(obj.valortamanho);
            });
          }
        );
});

In the query link you will be asked to return an array of JSON type size.

    
03.11.2015 / 15:04