Javascript 在下拉列表中更新输入值选择更改ajax和json

Javascript 在下拉列表中更新输入值选择更改ajax和json,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我正在学习使用PHP/MySQL数据库的AJAX和JSON。我有这样的情况: 我有4个下拉输入。它们都是从数据库检索结果的依赖项(类似于过滤器)。在最后一个下拉列表中,我选择了汽车发动机(马达)。当我选择引擎时,我想在发送之前通过AJAX用id填充一个隐藏的文本输入。这样我就可以将ID和帖子一起发送到PHP 我错过了什么 以下是我的PHP代码: //$_POST = array_map('utf8_decode', $_POST); if(isset($_POST)){ include_

我正在学习使用PHP/MySQL数据库的AJAX和JSON。我有这样的情况:

我有4个下拉输入。它们都是从数据库检索结果的依赖项(类似于过滤器)。在最后一个下拉列表中,我选择了汽车发动机(马达)。当我选择引擎时,我想在发送之前通过AJAX用id填充一个隐藏的文本输入。这样我就可以将ID和帖子一起发送到PHP

我错过了什么

以下是我的PHP代码:

//$_POST = array_map('utf8_decode', $_POST);
if(isset($_POST)){
    include_once "config.php";
    $retorno = array();
    $retorno['status'] = 0;
    $retorno['conteudo'] = '';
    $_SESSION['tabela'] = ($_POST['opcao'] == "seguimento") ? $_POST['seguimento'] : $_SESSION['tabela'];
    if($_SESSION['tabela'] == '4 rodas'){
        $tabela = '4rodas';
    }elseif($_SESSION['tabela'] == '2 rodas'){
        $tabela = '2rodas';
    }elseif($_SESSION['tabela'] == 'Diesel'){
        $tabela = 'diesel';
    }elseif($_SESSION['tabela'] == 'Sonda'){
        $tabela = 'sonda';
    }
    switch($_POST['opcao']){
        case 'seguimento':
        //pego o fabricante
            $seguimento = strip_tags(trim(filter_input(INPUT_POST, 'seguimento', FILTER_SANITIZE_STRING)));
            $pegaFabricante = $pdo->prepare("SELECT * FROM `".$tabela."` GROUP BY `fabricante`");
            $pegaFabricante->execute();
            if($pegaFabricante->rowCount() == 0){
                $retorno['status'] = 0;
            }else{
                $retorno['status'] = 1;
                $retorno['conteudo'] .= '<option value="" selected>Selecione o fabricante</option>';
                while($fabricante = $pegaFabricante->fetchObject()){
                    $retorno['conteudo'] .= '<option value="'.$fabricante->fabricante.'">'.$fabricante->fabricante.'</option>';
                }
            }
        break;

        case 'fabricante':
        //pego o veiculo
            $seguimento = strip_tags(trim(filter_input(INPUT_POST, 'seguimento', FILTER_SANITIZE_STRING)));
            $fabricante = strip_tags(trim(filter_input(INPUT_POST, 'fabricante', FILTER_SANITIZE_STRING)));

                $pegaVeiculo = $pdo->prepare("SELECT * FROM `".$tabela."` WHERE `fabricante` = ? GROUP BY `modelo`");
                $pegaVeiculo->execute(array($fabricante));

            if($pegaVeiculo->rowCount() == 0){
                $retorno['status'] = 0;
            }else{
                $retorno['status'] = 1;
                $retorno['conteudo'] .= '<option value="" selected>Selecione o veículo</option>';
                while($veiculo = $pegaVeiculo->fetchObject()){
                    $retorno['conteudo'] .=  '<option value="'.$veiculo->modelo.'">'.$veiculo->modelo.'</option>';

                }
            }
        break;

        case 'veiculo':
        //pego o motor
            $seguimento = strip_tags(trim(filter_input(INPUT_POST, 'seguimento', FILTER_SANITIZE_STRING)));
            $fabricante = strip_tags(trim(filter_input(INPUT_POST, 'fabricante', FILTER_SANITIZE_STRING)));
            $veiculo    = strip_tags(trim(filter_input(INPUT_POST, 'veiculo', FILTER_SANITIZE_STRING)));

            $pegaMotor = $pdo->prepare("SELECT * FROM `".$tabela."` WHERE `fabricante` = ? AND `modelo` = ? GROUP BY `motor`");
            $pegaMotor->execute(array($fabricante, $veiculo));
            if($pegaMotor->rowCount() == 0){
                $retorno['status'] = 0;
            }else{
                $retorno['status'] = 1;
                $retorno['conteudo'] .= '<option value="" selected>Selecione o motor</option>';
                while($motor = $pegaMotor->fetchObject()){
                    $retorno['conteudo'] .= '<option value="'.$motor->motor.'">'.$motor->motor.'</option>';
                }
            }
        break;

        case 'motor':
        //retorna o que selecinou
            $seguimento = strip_tags(trim(filter_input(INPUT_POST, 'seguimento', FILTER_SANITIZE_STRING)));
            $fabricante = strip_tags(trim(filter_input(INPUT_POST, 'fabricante', FILTER_SANITIZE_STRING)));
            $veiculo = strip_tags(trim(filter_input(INPUT_POST, 'veiculo', FILTER_SANITIZE_STRING)));
            $motor = strip_tags(trim(filter_input(INPUT_POST, 'motor', FILTER_SANITIZE_STRING)));
            $processo = strip_tags(trim(filter_input(INPUT_POST, 'n_processo', FILTER_SANITIZE_STRING)));

            $pegaAutomovel = $pdo->prepare("SELECT * FROM `".$tabela."` WHERE `fabricante` = ? AND `modelo` = ? AND `motor` = ?");
            $pegaAutomovel->execute(array($fabricante, $veiculo, $motor));
            if($pegaAutomovel->rowCount() == 0){
                $retorno['status'] = 0;
            }else{
                $retorno['status'] = 1;

                if($seguimento == "4 rodas"):
                        $retorno['conteudo'] .= '<thead>';
                            $retorno['conteudo'] .= '<tr class="table">';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza" valign="middle"><img src="images/veiculo.jpg"></td>';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center"><img src="images/motor.jpg"></td>';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza combustivel" style="background: #e7e7e9;"><img src="images/combustivel.jpg"></td>';
                                $retorno['conteudo'] .= '<td colspan="3" align="center" class="cinza" style="background: #ffcb05;"><img src="images/velas.jpg"></td>';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center"><img src="images/gap.jpg"></td>';
                                $retorno['conteudo'] .= '<td colspan="2" align="center" style="background: black;"><img src="images/cabos.jpg"></td>';
                                $retorno['conteudo'] .= '<td colspan="2" align="center" style="background: #ffcb05;"><img src="images/bobina-igni.jpg"></td>';
                            $retorno['conteudo'] .= '</tr">';

                            $retorno['conteudo'] .= '<tr class="table">';
                                $retorno['conteudo'] .= '<td class="cinza" align="center" valign="middle" style="background: #e7e7e9;"><img src="images/ano.jpg"></td>';
                                $retorno['conteudo'] .= '<td class="cinza" align="center" valign="middle" style="background: #ec1d25;"><img src="images/ngk.jpg"></td>';
                                $retorno['conteudo'] .= '<td class="cinza" align="center" valign="middle" style="background: #00a650;"><img src="images/ngk_green.jpg"></td>';
                                $retorno['conteudo'] .= '<td align="center" style="background: #e7e7e9;"><img src="images/ano.jpg"></td>';
                                $retorno['conteudo'] .= '<td align="center" style="background: #ec1d25;"><img src="images/cabo.jpg"></td>';
                                $retorno['conteudo'] .= '<td align="center" style="background: #e7e7e9;"><img src="images/ano.jpg"></td>';
                                $retorno['conteudo'] .= '<td align="center" style="background: #ec1d25;"><img src="images/bobina.jpg"></td>';
                            $retorno['conteudo'] .= '</tr">';


                        $retorno['conteudo'] .= '</thead>';
                    while($automovel = $pegaAutomovel->fetchObject()){

                        $retorno['conteudo'] .=  '<tr><td>'.$automovel->modelo.'</td><td>'.$automovel->motor.'</td>';
                        $retorno['conteudo'] .= '<td>'.$automovel->combustivel.'</td>';

                        $retorno['conteudo'] .= '<td>'.$automovel->ano_vela.'</td><td>'.$automovel->ngk.'</td><td>'.$automovel->ngk_green.'</td>';

                        $retorno['conteudo'] .= '<td>'.$automovel->gap.'</td>';

                        $retorno['conteudo'] .= '<td>'.$automovel->ano_cabo.'</td><td>'.$automovel->cabos_ngk.'</td>';
                        $retorno['conteudo'] .= '<td>'.$automovel->ano_bobina.'</td>';
                        $retorno['conteudo'] .= '<td>'.$automovel->bobina_ngk.'</td></tr>';
                    }

                    $retorno['conteudo'] .= '<thead>';
                        $retorno['conteudo'] .= '<tr class="table">';
                            $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza" valign="middle"><img src="images/veiculo.jpg"></td>';
                            $retorno['conteudo'] .= '<td rowspan="2" align="center"><img src="images/motor.jpg"></td>';
                            $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza combustivel" style="background: #e7e7e9;"><img src="images/combustivel.jpg"></td>';
                            $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza" style="background: #e7e7e9;"><img src="images/ano.jpg"></td>';
                            $retorno['conteudo'] .= '<td rowspan="2" align="center"><img style="max-width:50px;"src="images/p_sensor.jpg"></td>';
                            $retorno['conteudo'] .= '<td rowspan="5" align="center" style="background: #73F822;"><img style="max-width:150px;" src="images/o_sensor.jpg"></td>';        
                        $retorno['conteudo'] .= '</tr">';
                    $retorno['conteudo'] .= '</thead>';

                    $pegaSonda = $pdo->prepare("SELECT * FROM sonda WHERE n_processo = '$processo'");
                    $pegaSonda->execute();

                    while($lnC = $pegaSonda->fetchObject()){
                        $retorno['conteudo'] .= '<tr>';
                            $retorno['conteudo'] .= '<td>'.$lnC->modelo.'</td>';
                            $retorno['conteudo'] .= '<td>'.$lnC->motor_versao.'</td>';
                            $retorno['conteudo'] .= '<td>'.$lnC->combustivel.'</td>';
                            $retorno['conteudo'] .= '<td>'.$lnC->ano_seox.'</td>';
                            $retorno['conteudo'] .= '<td>'.$lnC->posicao_sensor.'</td>';
                            $retorno['conteudo'] .= '<td>'.$lnC->codigo_sensor.'</td>';
                        $retorno['conteudo'] .= '</tr>';
                    }



                elseif($seguimento == '2 rodas'):
                    $retorno['conteudo'] .= '<thead>';
                        $retorno['conteudo'] .= '<tr class="table">';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza" valign="middle"><img style="width: 120px;" src="images/cabem.jpg"></td>';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center"><img style="width: 120px;" src="images/modelo.jpg"></td>';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza combustivel" style="background: #e7e7e9;"><img src="images/combustivel.jpg"></td>';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza combustivel" style="background: #e7e7e9;"><img src="images/ano.jpg"></td>';
                                $retorno['conteudo'] .= '<td colspan="3" align="center" class="cinza" style="background: #ffcb05;"><img src="images/velas.jpg"></td>';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center" style="background: #e7e7e9;"><img style="width: 100px;" src="images/terminais.jpg"></td>';


                            $retorno['conteudo'] .= '</tr">';

                            $retorno['conteudo'] .= '<tr class="table">';
                                $retorno['conteudo'] .= '<td class="cinza" align="center" valign="middle" style="background: #ec1d25;"><img src="images/ngk.jpg"></td>';
                                $retorno['conteudo'] .= '<td class="cinza" align="center" valign="middle" style="background: #e7e7e9;"><img style="width: 60px;" src="images/iridum.jpg"></td>';
                                $retorno['conteudo'] .= '<td class="cinza" align="center" valign="middle" style="background: white;"><img style="width: 80px;" src="images/luz.jpg"></td>';

                            $retorno['conteudo'] .= '</tr">';
                    $retorno['conteudo'] .= '</thead>';
                    while($automovel = $pegaAutomovel->fetchObject()){
                            $retorno['conteudo'] .= '<tr>';
                                $retorno['conteudo'] .= '<td>'.$automovel->motor.'</td>';
                                $retorno['conteudo'] .= '<td>'.$automovel->modelo.'</td>';
                                $retorno['conteudo'] .= '<td>'.$automovel->combustivel.'</td>';
                                $retorno['conteudo'] .= '<td>'.$automovel->ano_vela.'</td>';
                                $retorno['conteudo'] .= '<td>'.$automovel->ngk.'</td>';
                                $retorno['conteudo'] .= '<td>'.$automovel->ngk_iridium.'</td>';
                                $retorno['conteudo'] .= '<td>'.$automovel->gap.'</td>';
                                $retorno['conteudo'] .= '<td>'.$automovel->cabos_ngk.'</td>';
                            $retorno['conteudo'] .= '/<tr>';

                    }

                    elseif($seguimento == "Diesel"):
                        $retorno['conteudo'] .= '<thead>';
                            $retorno['conteudo'] .= '<tr class="table">';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza" valign="middle"><img src="images/veiculo.jpg"></td>';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center"><img src="images/motor.jpg"></td>';
                                $retorno['conteudo'] .= '<td rowspan="2" align="center" class="cinza combustivel" style="background: #e7e7e9;"><img src="images/ano.jpg"></td>';
                                $retorno['conteudo'] .= '<td colspan="3" align="center" class="cinza" style="background: red;"><img style="width: 200px;" src="images/vela_aquecer.jpg"></td>';
                            $retorno['conteudo'] .= '</tr">';




                        $retorno['conteudo'] .= '</thead>';
                    while($automovel = $pegaAutomovel->fetchObject()){

                        $retorno['conteudo'] .=  '<tr><td>'.$automovel->modelo.'</td><td>'.$automovel->motor.'</td>';
                        $retorno['conteudo'] .= '<td>'.$automovel->ano_vela.'</td><td>'.$automovel->codigo_g.'</td><td></tr>';
                    }   
                endif;  
            }
        break;
    }

    echo json_encode($retorno);
}
/$\u POST=array\u map('utf8\u decode',$\u POST);
国际单项体育联合会(国际单项体育联合会){
包括一次“config.php”;
$returno=array();
$RETRONO['status']=0;
$returno['conteudo']='';
$会话['tabela']=($POST['opcao']='seguimento”)?$POST['seguimento']:$会话['tabela'];
如果($\会话['tabela']=='4 rodas'){
$tabela='4rodas';
}elseif($_会话['tabela']=='2 rodas'){
$tabela='2rodas';
}elseif($\会话['tabela']=='Diesel'){
$tabela='diesel';
}elseif($\会话['tabela']=='Sonda'){
$tabela='sonda';
}
开关($_POST['opcao']){
案例“seguimento”:
//pego-o-fabricante
$seguimento=strip_标签(修剪(过滤输入(输入后,'seguimento',过滤消毒字符串));
$pegaFabricante=$pdo->prepare(“从“.”$tabela.”“按`fabricante`分组”);
$pegaFabricante->execute();
如果($pegaFabricante->rowCount()==0){
$RETRONO['status']=0;
}否则{
$returno['status']=1;
$returno['conteudo'].='Selecione o fabricante';
而($fabricante=$pegaFabricante->fetchObject()){
$returno['conteudo'].='.$fabricante->fabricante.';
}
}
打破
“制造者”一案:
//佩戈·维库洛
$seguimento=strip_标签(修剪(过滤输入(输入后,'seguimento',过滤消毒字符串));
$fabricante=strip_标签(修剪(过滤输入(输入后,'fabricante',过滤消毒字符串));
$pegaVeiculo=$pdo->prepare(“从`.$tabela.`WHERE`fabricante`=?按`modelo`'分组选择*”;
$pegaVeiculo->execute(数组($fabricante));
如果($pegaVeiculo->rowCount()==0){
$RETRONO['status']=0;
}否则{
$returno['status']=1;
$returno['conteudo'].='Selecione o veículo';
而($veiculo=$pegaVeiculo->fetchObject()){
$returno['conteudo'].='.$veiculo->modelo.';
}
}
打破
“veiculo”案例:
//pego电机
$seguimento=strip_标签(修剪(过滤输入(输入后,'seguimento',过滤消毒字符串));
$fabricante=strip_标签(修剪(过滤输入(输入后,'fabricante',过滤消毒字符串));
$veiculo=strip_标签(修剪(过滤输入(输入后,'veiculo',过滤消毒字符串));
$pegaMotor=$pdo->prepare(“从“.”$tabela.“`WHERE`fabricante`=”和`modelo`=”中选择*并按`motor`分组”;
$pegaMotor->execute(数组($fabricante,$veiculo));
如果($pegaMotor->rowCount()==0){
$RETRONO['status']=0;
}否则{
$returno['status']=1;
$returno['conteudo'].='Selecione o motor';
而($motor=$pegaMotor->fetchObject()){
$returno['conteudo'].='.$motor->motor'.';
}
}
打破
“电机”箱:
//塞莱西努酒店
$seguimento=strip_标签(修剪(过滤输入(输入后,'seguimento',过滤消毒字符串));
$fabricante=strip_标签(修剪(过滤输入(输入后,'fabricante',过滤消毒字符串));
$veiculo=strip_标签(修剪(过滤输入(输入后,'veiculo',过滤消毒字符串));
$motor=带标签(修剪(过滤器输入(“电机”输入,过滤器消毒字符串));
$processo=strip_标签(trim(过滤器输入(输入后,'n_processo',过滤器消毒字符串));
$pegaAutomovel=$pdo->prepare(“从`.$tabela.`WHERE`fabricante`=?和`modelo`=?和`motor`=?”中选择*”;
$pegaAutomovel->execute(数组($fabricante、$veiculo、$motor));
如果($pegaAutomovel->rowCount()==0){
$RETRONO['status']=0;
}否则{
$returno['status']=1;
如果($seguimento==“4 rodas”):
$returno['conteudo']。='';
$returno['conteudo']。='';
$returno['conteudo']。='';
$returno['conteudo']。='';
$returno['conteudo']。='';
$returno['conteudo']。='';
$returno['conteudo']。='';
$returno['conteudo']。='';
$returno['conteudo']。='';
$returno['conteudo']。='';
$returno['conteudo']。='';
而($automovel=$pegaAutomovel->fetchObject()){
$returno['conteudo'].='.$automovel->modelo.'.$automovel->motor.';
$returno['conteudo'].='.$automovel->combilivel.';
$returno['conteudo'].='.$automovel->ano_vela.'.$automovel->ngk.'.$automovel->ngk_green.'.$automovel->ngk_green.';
$returno['conteudo'].='.$automovel->gap.';
$returno['conteudo'].='.$automovel->ano_-cabo.'.$automovel->cabos_-ngk.';
$returno['conteudo'].='.$automovel->ano_bobina'.';
$returno['conteudo'].='.$automovel->bobina_ngk'.';
}
$returno['conteudo']。='';
$(function(){
    $('select[name=seguimento]').change(function(){
        var seguimento = $(this).val();
        $('select[name=fabricante]').html('<option value="" selected>Selecione o fabricante</option>');
        $('select[name=veiculo]').html('<option value="" selected>Selecione o veículo</option>');
        $('select[name=motor]').html('<option value="" selected>Selecione o Motor</option>');

        $.ajax({
            method: 'post',
            url: 'combo.php',
            data:{opcao: 'seguimento', seguimento: seguimento},
            dataType: 'json',
            beforeSend: function(){
                $('#status').html('Aguarde, buscando...');
                $('table#conteudo').html('');
            },
            success: function(retorno){
                if(retorno.status == 0){
                    $('#status').html('Não encontramos fabricantes!');
                    $('table#conteudo').html();
                }else{
                    $('#status').html('');
                    $('select[name=fabricante]').html(retorno.conteudo);
                }
            }
        });
    });

    $('select[name=fabricante]').change(function(){
        var fabricante = $(this).val();
        var seguimento = $('select[name=seguimento]').val();
        var motor = $('select[name=motor]').val();

        $.ajax({
            method: 'post',
            url: 'combo.php',
            data:{opcao: 'fabricante', fabricante: fabricante, seguimento: seguimento},
            dataType: 'json',
            beforeSend: function(){
                $('#status').html('Aguarde, buscando...');
                $('table#conteudo').html('');
            },
            success: function(retorno){
                if(retorno.status == 0){
                    $('#status').html('Não encontramos os veículos!');
                }else{
                    $('#status').html('');
                    $('select[name=veiculo]').html(retorno.conteudo);
                }
            }
        });
    });

    $('select[name=veiculo]').change(function(){
        var fabricante = $('select[name=fabricante]').val();
        var seguimento = $('select[name=seguimento]').val();
        var motor = $('select[name=motor]').val();
        var veiculo = $(this).val();
        $.ajax({
            method: 'post',
            url: 'combo.php',
            data:{opcao: 'veiculo', fabricante: fabricante, seguimento: seguimento, veiculo: veiculo},
            dataType: 'json',
            beforeSend: function(){
                $('#status').html('Aguarde, buscando...');
                $('table#conteudo').html('');
            },
            success: function(retorno){
                if(retorno.status == 0){
                    $('#status').html('Não encontramos os Motores!');
                }else{
                    $('#status').html('');
                    $('select[name=motor]').html(retorno.conteudo);
                }
            }
        });
    });

    $('select[name=motor]').change(function(){
        var fabricante = $('select[name=fabricante]').val();
        var seguimento = $('select[name=seguimento]').val();
        var veiculo = $('select[name=veiculo]').val();
        var motor = $(this).val();

        $.ajax({
            method: 'post',
            url: 'combo.php',
            data:{opcao: 'motor', fabricante: fabricante, seguimento: seguimento, veiculo: veiculo, motor: motor, n_processo: n_processo},
            dataType: 'json',
            beforeSend: function(){
                $('#status').html('Aguarde, buscando...');
            },
            success: function(retorno){
                $('#status').html('');
                $('table#conteudo').html(retorno.conteudo);
                $('input[name=n_processo]').val(n_processo.n_processo);
                console.log(n_processo)
            }
        });
    });
});