Advertisement
RobsonAlexandre

Combos Dinâmicos com jQuery

Dec 4th, 2012
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.19 KB | None | 0 0
  1. <?php
  2.  
  3. Class mysql {
  4.     public function Conectar() {
  5.         if (mysql_connect('localhost', 'root', '')) {
  6.             if (!mysql_select_db('teste')) {
  7.                 die('Não foi possível localizar database "teste"');
  8.             }
  9.         } else {
  10.             die('Não foi possível conectar: ' . mysql_error());
  11.         }
  12.         return $this;
  13.     }
  14.  
  15.     public function ExecutarConsulta($sql) {
  16.         return mysql_query($sql);
  17.     }
  18.  
  19.     public function Desconectar() {
  20.         mysql_close();
  21.     }
  22. }
  23.  
  24. if (isset($_POST['hddSubmit'])) {
  25.     $tipo = $_POST['tipo'];
  26.     $marca = $_POST['marca'];
  27.     $modelo = $_POST['modelo'];
  28. }
  29. ?>
  30.  
  31. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  32.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  33. <html xmlns="http://www.w3.org/1999/xhtml">
  34.     <head>
  35.         *<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>*
  36.         <script type="text/javascript">
  37.             $(document).ready(function(){
  38.             // Evento change no campo tipo
  39.             $("select[name=tipo]").change(function(){
  40.             // Exibimos no campo marca antes de concluirmos
  41.             $("select[name=marca]").html('<option value="">Carregando...</option>');
  42.             // Exibimos no campo marca antes de selecionamos a marca, serve também em caso
  43.             // do usuario ja ter selecionado o tipo e resolveu trocar, com isso limpamos a
  44.             // seleção antiga caso tenha feito.
  45.             $("select[name=modelo]").html('<option value="">Aguardando marca...</option>');
  46.             // Passando tipo por parametro para a pagina ajax-marca.php
  47.             $.post("ajax-marca.php",
  48.             {tipo:$(this).val()},
  49.             // Carregamos o resultado acima para o campo marca
  50.                   function(valor){
  51.             $("select[name=marca]").html(valor);
  52.             }
  53.             )
  54.             })
  55.         // Evento change no campo marca
  56.         $("select[name=marca]").change(function(){
  57.             // Exibimos no campo modelo antes de concluirmos
  58.         $("select[name=modelo]").html('<option value="">Carregando...</option>');
  59.             // Passando marca por parametro para a pagina ajax-modelo.php
  60.             $.post("ajax-modelo.php",
  61.             {marca:$(this).val()},
  62.             // Carregamos o resultado acima para o campo modelo
  63.                   function(valor){
  64.             $("select[name=modelo]").html(valor);
  65.             }
  66.             )
  67.             })
  68.       })
  69.         </script>
  70.         <title> 3 combos dinamicos</title>
  71.     </head>
  72.     <body>
  73.         *<form action="index.php" method="post" name="carros" id="auto">*
  74.  
  75.             <input type="hidden" name="hddSubmit" id="hddSubmit" value="submiForm" />
  76.             <select name="tipo" id="tipo">
  77.                 <option value="null">Escolher tipo</option>
  78.                 <?php
  79.                 $conn = new mysql;
  80.                 $conn->Conectar();
  81.                 $sql = "SELECT * FROM tbl_tipo ORDER BY tipo ASC";
  82.                 $qr = $conn->ExecutarConsulta($sql);
  83.                 while ($ln = mysql_fetch_assoc($qr)) {
  84.                     $selected = ($ln['t'] == $tipo) ? 'Selected="selected"' : '';
  85.                     echo '<option value="' . $ln['t'] . '" ' . $selected . '>' . $ln['tipo'] . '</option>';
  86.                 }
  87.                 ?>
  88.             </select>
  89.             &nbsp;&nbsp;
  90.             <select name="marca" id="marca">                
  91.                 <?
  92.                 if (isset($marca) || isset($tipo)) {
  93.                     include('ajax-marca.php');
  94.                 } else {
  95.                     echo '<option value="0" >Aguardando tipo...</option>';
  96.                 }
  97.                 ?>
  98.             </select>
  99.             &nbsp;&nbsp;
  100.             <select name="modelo" id="modelo">
  101.                 <?
  102.                 if (isset($marca) || isset($modelo)) {
  103.                     include('ajax-modelo.php');
  104.                 } else {
  105.                     echo '<option value="0" >Aguardando marca...</option>';
  106.                 }
  107.                 ?>
  108.             </select>
  109.             &nbsp;&nbsp;
  110.             <input type="submit" name="btnOk" value="pesquisar" />
  111.  
  112.             *</form>*
  113.         <?php $conn->Desconectar(); ?>
  114.     </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement