Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- Class mysql {
- public function Conectar() {
- if (mysql_connect('localhost', 'root', '')) {
- if (!mysql_select_db('teste')) {
- die('Não foi possível localizar database "teste"');
- }
- } else {
- die('Não foi possível conectar: ' . mysql_error());
- }
- return $this;
- }
- public function ExecutarConsulta($sql) {
- return mysql_query($sql);
- }
- public function Desconectar() {
- mysql_close();
- }
- }
- if (isset($_POST['hddSubmit'])) {
- $tipo = $_POST['tipo'];
- $marca = $_POST['marca'];
- $modelo = $_POST['modelo'];
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- *<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>*
- <script type="text/javascript">
- $(document).ready(function(){
- // Evento change no campo tipo
- $("select[name=tipo]").change(function(){
- // Exibimos no campo marca antes de concluirmos
- $("select[name=marca]").html('<option value="">Carregando...</option>');
- // Exibimos no campo marca antes de selecionamos a marca, serve também em caso
- // do usuario ja ter selecionado o tipo e resolveu trocar, com isso limpamos a
- // seleção antiga caso tenha feito.
- $("select[name=modelo]").html('<option value="">Aguardando marca...</option>');
- // Passando tipo por parametro para a pagina ajax-marca.php
- $.post("ajax-marca.php",
- {tipo:$(this).val()},
- // Carregamos o resultado acima para o campo marca
- function(valor){
- $("select[name=marca]").html(valor);
- }
- )
- })
- // Evento change no campo marca
- $("select[name=marca]").change(function(){
- // Exibimos no campo modelo antes de concluirmos
- $("select[name=modelo]").html('<option value="">Carregando...</option>');
- // Passando marca por parametro para a pagina ajax-modelo.php
- $.post("ajax-modelo.php",
- {marca:$(this).val()},
- // Carregamos o resultado acima para o campo modelo
- function(valor){
- $("select[name=modelo]").html(valor);
- }
- )
- })
- })
- </script>
- <title> 3 combos dinamicos</title>
- </head>
- <body>
- *<form action="index.php" method="post" name="carros" id="auto">*
- <input type="hidden" name="hddSubmit" id="hddSubmit" value="submiForm" />
- <select name="tipo" id="tipo">
- <option value="null">Escolher tipo</option>
- <?php
- $conn = new mysql;
- $conn->Conectar();
- $sql = "SELECT * FROM tbl_tipo ORDER BY tipo ASC";
- $qr = $conn->ExecutarConsulta($sql);
- while ($ln = mysql_fetch_assoc($qr)) {
- $selected = ($ln['t'] == $tipo) ? 'Selected="selected"' : '';
- echo '<option value="' . $ln['t'] . '" ' . $selected . '>' . $ln['tipo'] . '</option>';
- }
- ?>
- </select>
-
- <select name="marca" id="marca">
- <?
- if (isset($marca) || isset($tipo)) {
- include('ajax-marca.php');
- } else {
- echo '<option value="0" >Aguardando tipo...</option>';
- }
- ?>
- </select>
-
- <select name="modelo" id="modelo">
- <?
- if (isset($marca) || isset($modelo)) {
- include('ajax-modelo.php');
- } else {
- echo '<option value="0" >Aguardando marca...</option>';
- }
- ?>
- </select>
-
- <input type="submit" name="btnOk" value="pesquisar" />
- *</form>*
- <?php $conn->Desconectar(); ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement