Keep BD output when select with jquery

Asked

Viewed 51 times

-2

I have a form that shows the lines(departments) and the families(categories) of products. I’m trying to improve the register by populating the family combobox, so select the line, returning the families that are linked to the selected line. I can show the right div when it is product inclusion, but when it is to update, the families combobox is not displayed.

Follows my code:

php lines.

<?php
include '../url_seo.php';
include 'config/config.inc.php';
include "funcoes/conexao.php";

conecta();

$cd_itprod = '786454';

$query = "select * from ti_produtos where cd_itprod= '".$cd_itprod."'";
$resul = mysql_query($query) or die (mysql_error());
$row   = mysql_fetch_object($resul);
$cd_linha   = $row->cd_linha;
$cd_familia = $row->cd_familia;
?>
<html>
  <head>
    <title>Atualizando combos com jquery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="<?= $url_seo; ?>js/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#cd_linha').change(function(){
            $('#familia').load('listaFamilias.php?cd_linha='+$('#cd_linha').val());
        });
    });
    </script>
  </head>
  <body>
  <h1>Atualizando combos com jquery</h1>
    <label>cd_linha:</label>
    <select name="cd_linha" id="cd_linha">
        <?php 
        $rs = mysql_query("SELECT * FROM ti_linhas ORDER BY nm_linha");
        while($reg = mysql_fetch_object($rs)): ?>
        <option value="<?php echo $reg->cd_linha ?>"><?php echo $reg->nm_linha ?></option>
    <?php endwhile; ?>
    </select>
    <br /><br />
    <div id="familia"></div>
  </body>
</html>

listaFamilias.php

<?php 
include '../url_seo.php';
include 'config/config.inc.php';
include "funcoes/conexao.php";

conecta();

$cd_linha = $_GET['cd_linha'];

$rs = mysql_query("SELECT * FROM ti_familias WHERE cd_linha = '$cd_linha' ORDER BY nm_familia");

echo "<label>familias: </label><select name='familia'>";
while($reg = mysql_fetch_object($rs)){
    echo "<option value='$reg->cd_familia'>$reg->nm_familia</option>";
}
echo "</select>";

How do I make the combobox that is listed in Friends.php already filled with database data?

  • $('#familia').load will attempt to place a new select inside your select, which is invalid. You will need to create an element outside of select to use .load. Or in php to send only options.

  • Thanks for the reply @bfavaretto, but I didn’t understand.

1 answer

0

I was able to solve it by showing a combobox when the $cd_familia variable already exists, if the user changes it, looks for the combobox in listFamilias.php and gives Hide in the div "com_familia":

<?php
include '../url_seo.php';
include 'config/config.inc.php';
include "funcoes/conexao.php";
include "funcoes/funcoes.php";

conecta();

$cd_itprod = '786454';
echo "produto: ".$cd_itprod."<br>";

$query = "select * from ti_produtos where cd_itprod= '".$cd_itprod."'";
$resul = mysql_query($query) or die (mysql_error());
$row   = mysql_fetch_object($resul);
$cd_linha   = $row->cd_linha;
$cd_familia = $row->cd_familia;

echo "cd_linha: ".$cd_linha. " - ".ds_linha($cd_linha)." - cd_familia: ".$cd_familia." - ".ds_familia($cd_familia)."<br>";
?>
<html>
  <head>
    <title>Atualizando combos com jquery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="<?= $url_seo; ?>js/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#cd_linha').change(function(){
            $("#com_familia").hide();
            $('#familia').load('listaFamilias.php?cd_linha='+$('#cd_linha').val());
        });
    });
    </script>
  </head>
  <body>
  <h1>Atualizando combos com jquery</h1>
    <label>cd_linha:</label>
    <?php 
    $rs = mysql_query("SELECT * FROM ti_linhas ORDER BY nm_linha");
    ?>
    <select name="cd_linha" id="cd_linha">
    <?php 
        while($reg = mysql_fetch_object($rs)): 
            echo "<option value='".$reg->cd_linha."'".($cd_linha == $reg->cd_linha?"SELECTED":"").">".$reg->nm_linha." (cód: ".$reg->cd_linha.")";
        endwhile; ?>
    </select>
    <br /><br />
    <div id="com_familia">
    <?
    if(isset($cd_familia)):?>
        <?
        $queryg = "select * from ti_familias where cd_familia!='' order by nm_familia";
        echo "<label>familias: </label><select name='cd_familia'>";
        $resug  = mysql_query($queryg) or die (mysql_error());
        while($rowg = mysql_fetch_object($resug)):
            echo "<option value='".$rowg->cd_familia."'".($cd_familia == $rowg->cd_familia?"SELECTED":"").">".$rowg->nm_familia." (cod: ".$rowg->cd_familia." - Linha: ".ds_linha($rowg->cd_linha).")";
        endwhile;
        echo "</select>";
    endif;
    ?>
    </div>
    <div id="familia"></div>
  </body>
</html>

Browser other questions tagged

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