Ajax and PHP form open dynamic list with SQL query

Asked

Viewed 60 times

2

Friends, good afternoon!

I need to edit a form that was already ready, including some new options. I know almost nothing about PHP, and I’m having a hard time getting the form combobox to return an SQL query.

This form was developed with PHP, AJAX and SQL.

There are 3 files for the data to appear on the web page.

-classifier_HelpCenter.php -classifier_HelpCenter_ajax.php -classifier_HelpCenter_sql.php

This form uses Bootstrap (What I don’t understand either, unfortunately)

The form had already configured, the fields (User, Requester and Requester Name), the field I am trying to include is "Service".

I will enter the code of the AJAX and SQL files, so that you can understand better and maybe you can help me.

-classifier_HelpCenter.php

<!DOCTYPE html>
<?PHP 
    header('Content-Type: text/html; charset=ISO-8859-1');
    define( "RAIZ_augusta", "//57.228.131.75/" );
    require_once("//57.228.131.75/icc/funciones/funciones.php" );
    require_once("../../complemento/complemento.php");
    $ua = getBrowser();
    if (strpos($ua['name'],'Internet') === false){
        include_once("../../complemento/analyticstracking.php");
    }
    session_start();


    if(@array_key_exists("34", $_SESSION[ "modulos" ])){ 
?>

<html>
<head>
    <title>Augusta | Classificador Help Center</title>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=9; IE=8; IE=7; IE=EDGE'/>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>

    <!-- CSS -->
        <!-- bootStrap -->
        <link rel='stylesheet' href='<?php echo RAIZ ?>/css/bootstrap.css'>
        <link rel='stylesheet' href='<?php echo RAIZ ?>/css/font-awesome/css/font-awesome.min.css'>
    <!-- jQuery --> 
        <script src='<?php echo RAIZ_augusta ?>icc/js/jquery-1.9.1.min.js'></script>    
        <!-- bootStrap -->
        <script src='<?php echo RAIZ ?>/js/bootstrap.js'></script>
    <!--[if IE]>
        <script src='<?php echo RAIZ_augusta ?>icc/js/html5.js'></script>
        <link href='<?php echo RAIZ ?>/css/bootstrap-ie7.css' rel='stylesheet'>
        <script src="<?php echo RAIZ ?>/js/html5shiv.js"></script>
        <script src="<?php echo RAIZ ?>/js/respond.min.js"></script>
    <![endif]-->
    <!--[if IE 8]>
        <link rel='stylesheet' type='text/css' media='all' href='<?php echo RAIZ_augusta ?>icc/css/icc_ie8.css'/>
    <![endif]-->
    <!--[if lte IE 7]>
        <link rel='stylesheet' type='text/css' media='all' href='<?php echo RAIZ_augusta ?>icc/css/ie.css'/>
        <script type='text/javascript' src='<?php echo RAIZ_augusta ?>icc/js/IE8.js'></script>
    <![endif]-->
    <!--[if lt IE 7]>
        <link rel='stylesheet' type='text/css' media='all' href='<?php echo RAIZ_augusta ?>icc/css/ie6.css'/>
    <![endif]-->

    <script>
        $( document ).ready(function(){
            carregar_form(); 
        });
    </script>
    <script> <!-- carregar form -->
        function carregar_form() {
            if( true ) {
                $.ajax({
                    url: "classificador_HelpCenter_ajax.php", 
                    dataType: "html",
                    type: "POST", 
                    data: { 
                        'seccion': 'carregarForm'   
                    }
                    ,beforeSend: function(){
                        $("#form_classificador").html("<div class='row text-center' style='margin-top:15px;'><img src='http://57.228.131.75/augusta/img/miniload.gif' alt='Carregando'/><div>"); 
                    }
                    ,success: function( data ) {
                        $("#form_classificador").html( data ); 
                        document.getElementById('filtro_Solicitante').value = "";
                    }
                })
            }           
        }
    </script>
    <script> <!-- carrega Atendimento -->
        function carregarAtendimento() {
            if( true ) {
                $.ajax({
                    url: "classificador_HelpCenter_ajax.php", 
                    dataType: "html",
                    type: "POST", 
                    data: { 
                        'seccion': 'carregarAtendimento'    
                    }
                    ,beforeSend: function(){
                        $("#form_classificador").html("<div class='row text-center' style='margin-top:15px;'><img src='http://57.228.131.75/augusta/img/miniload.gif' alt='Carregando'/><div>"); 
                    }
                    ,success: function( data ) {
                        $("#form_classificador").html( data ); 
                        document.getElementById('filtro_Atendimento').value = "";
                    }
                })
            }           
        }
    </script>
    <script> <!-- carrega subSolicitante -->
        function carregarSubSolicitante() {
            if( true ) {
                $.ajax({
                    url: "classificador_HelpCenter_ajax.php", 
                    dataType: "html",
                    type: "POST", 
                    data: { 
                        'seccion': 'carregarSubSolicitante',
                        'Solicitante'   : $('#filtro_Solicitante').val()
                    }
                    ,beforeSend: function(){
                        $("#div_subSolicitante").html("<div class='row text-center' style=''><img style='height:36px;' src='http://57.228.131.75/augusta/img/loadbar.gif'/><div>"); 
                    }
                    ,success: function( data ) {
                        $("#div_subSolicitante").html( data ); 
                        document.getElementById('filtro_SubSolicitante').value = "";
                    }
                })
            }           
        }
    </script>
    <script> <!-- function guardar e limpar -->
        function guardar(){
            if (  $( "#filtro_Atendimento" ).val() == null){
                alert("Por favor, informe o Canal de Atendimento");
                $("#filtro_Atendimento").focus();
                return false;
            }
            if (  $( "#filtro_Solicitante" ).val() == null){
                alert("Por favor, informe o Solicitante da ligação");
                $("#filtro_Solicitante").focus();
                return false;
            }
            if (  $( "#filtro_SubSolicitante" ).val() == null){
                alert("Por favor, informe o nome do Solicitante da ligação");
                $("#filtro_SubSolicitante").focus();
                return false;
            }
            if(true) {
                $.ajax({
                    url: "classificador_HelpCenter_ajax.php", 
                    dataType: "html",
                    type: "POST", 
                    data: { 
                        'seccion'   : 'guardar',
                        'Atendimento'       : $( "#filtro_Atendimento").val(),
                        'Solicitante'       : $( "#filtro_Solicitante" ).val(),
                        'subSolicitante'    : $( "#filtro_SubSolicitante").val()
                                            } 
                    ,beforeSend: function(){
                        $("#retorno").html("<div class='row text-center' style='margin-top:15px;'><img src='http://57.228.131.75/augusta/img/miniload.gif' alt='Carregando'/><div>"); 
                    }
                    ,success: function( data ) {
                        $( "#retorno" ).html( data ); 
                    }
                })
            }
        }
        function limpar(){
            carregar_form();
        };
    </script>
    <style>
        .titulo {
            font-size: 24px;
            color: #0054bf; 
            display: block;
            border-bottom: 1px solid rgba(163, 163, 163, 0.3);
            margin-bottom: 15px;
            padding-bottom: 5px;
            text-align: center;
        }
    </style>
</head>
<body>
    <!--[if IE]><?php imprimeMenuHeaderAugustaIE('faleAtendimento', 'Classif', @$_SESSION[ "bp" ])?><![endif]-->
    <!--[if ! IE]><!--><?php imprimeMenuHeaderAugusta('faleAtendimento', 'Classif', @$_SESSION[ "bp" ]) ?><!--<![endif]-->

    <section id="content">
        <section id="main"> 
            <div id="form_classificador"></div>
        </section>
    </section>

</body>
</html>
<?php 
    } else { 
        echo "<script type='text/javascript'>window.location.href='".RAIZ."';</script>";
    }
?>  

-classifier_HelpCenter_ajax.php

<?php
    header('Content-Type: text/html; charset=ISO-8859-1');
    include 'classificador_HelpCenter_sql.php';
    if(!isset($_SESSION)){session_start();}

    $seccion = $_POST["seccion"];
    @$Atendimento = $_POST['Atendimento'];
    @$Solicitante = $_POST['Solicitante'];
    @$subSolicitante = $_POST['subSolicitante'];

    if($seccion == "carregarForm"){ carregarForm();}
    if($seccion == "carregarSubSolicitante"){ carregarSubSolicitante($Solicitante);}
    if($seccion == "guardar"){ guardar($Atendimento, $Solicitante, $subSolicitante);}
    if($seccion == "carregarAtendimento"){ carregarAtendimento($Atendimento);}

function carregarForm(){


    $Solicitantes = obtiene_filtros('Solicitante', null);

    $out = '<div class="panel panel-default col-lg-offset-4 col-lg-4 col-md-offset-3 col-md-6 col-sm-offset-3 col-sm-6 col-xs-8 col-xs-offset-2" style="padding:0px;">
                <div class="panel-body" id="bodyForm">
                    <form class="form-horizontal">';
                        $out.='<fieldset>
                            <legend>Classificador Help Center</legend>';
                            if(true){ //NomeOperador
                                $out.='<div class="form-group">
                                    <label for="filtro_NomeOperador" class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label">Usuário</label>
                                    <div class="col-lg-9 col-md-8 col-sm-9 col-xs-9" style="padding-left:0px;">
                                        <input type="text" class="form-control" id="filtro_Nome" disabled value="'.$_SESSION['nomeGuerra'].'">
                                    </div>
                                </div>';
                            }
                            if(true){//Atendimento
                                $out.='<div class="form-group">
                                    <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label" for="filtro_Atendimento">Atendimento</label>
                                    <div class="col-lg-9 col-md-8 col-sm-9 col-xs-9" style="padding-left:0px;">
                                        <select class="form-control" id="filtro_Atendimento" onchange="carregarAtendimento($Atendimento);"></select>
                                    </div>
                                </div>';
                            }
                            if(true){//Solicitante
                                $out.='<div class="form-group">
                                    <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label" for="filtro_Solicitante">Solicitante</label>
                                    <div class="col-lg-9 col-md-8 col-sm-9 col-xs-9" style="padding-left:0px;">
                                        <select class="form-control" id="filtro_Solicitante" onchange="carregarSubSolicitante();">';
                                            foreach ($Solicitantes as $v){
                                                $out.='<option value="'.utf8_decode($v['valor']).'">'.utf8_decode($v['valor']).'</option>';
                                            }
                                        $out.='</select>
                                    </div>
                                </div>';
                            }
                            if(true){ //subSolicitante
                                $out.='<div class="form-group" id="div_subSolicitante">
                                    <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label" for="filtro_SubSolicitante">Nome</label>
                                    <div class="col-lg-9 col-md-8 col-sm-9 col-xs-9" style="padding-left:0px;">
                                        <select class="form-control" id="filtro_SubSolicitante"></select>
                                    </div>
                                </div>';
                            }
                            $out.='</fieldset>';
                        if(true){ //botões
                            $out.='<fieldset style="margin-top: 15px;" id="retorno">
                                <div class="col-lg-3 col-lg-offset-2 col-md-3 col-md-offset-2 col-sm-4 col-sm-offset-2 col-xs-4 col-xs-offset-2">
                                    <div class="btn btn-sm btn-default" onclick="limpar()"><i class="fa fa-fw fa-lg fa-close"></i>&nbsp;Cancelar</div>
                                </div>
                                <div class="col-lg-3 col-lg-offset-2 col-md-3 col-md-offset-2 col-sm-4 col-sm-offset-1 col-xs-4 col-xs-offset-1">
                                    <div class="btn btn-sm btn-primary" onclick="guardar()"><i class="fa fa-fw fa-lg fa-save"></i>&nbsp;Salvar</div>
                                </div>
                            </fieldset>';
                        }
                    $out.='</form>
                </div>
            </div>';

echo $out;
}
function carregarSubSolicitante($Solicitante){

    $filtro_duvida = obtiene_filtros('subSolicitante',utf8_decode($Solicitante));

    $out = '<label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label" for="filtro_SubSolicitante">Nome</label>
    <div class="col-lg-9 col-md-8 col-sm-9 col-xs-9" style="padding-left:0px;">
        <select class="form-control" id="filtro_SubSolicitante">';
            foreach ($filtro_duvida as $v){
                $out .= "<option value='".utf8_decode($v['valor'])."'>".utf8_decode($v['valor'])."</option>";
            }
        $out.='</select>
        </div>
    </div>';
echo $out;
}



function carregarAtendimento($Atendimento){

        $filtro = obtiene_Atendimento($Atendimento);

        $out = '<label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label" for="filtro_Atendimento">Atendimento</label>
    <div class="col-lg-9 col-md-8 col-sm-9 col-xs-9" style="padding-left:0px;">
        <select class="form-control" id="filtro_Atendimento">';
            foreach ($filtro as $v){
                $out .= "<option value='".utf8_decode($v['valor'])."'>".utf8_decode($v['valor'])."</option>";
            }
        $out.='</select>
        </div>
    </div>';
echo $out;
}
function guardar($Atendimento, $Solicitante, $subSolicitante){
        if(guardarResposta(utf8_decode($Atendimento),utf8_decode($Solicitante),utf8_decode($subSolicitante))){
            $out = '<div class="alert alert-success text-center col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-10 col-xs-offset-1">
                Dados salvos com sucesso.
            </div>
            <div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-10 col-xs-offset-1">
                <div class="btn btn-sm btn-default col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-10 col-xs-offset-1" onclick="limpar()">
                    <i class="fa fa-fw fa-lg fa-plus-square-o"></i>&nbsp;Novo
                </div>
            </div>';
        } else {
            $out = '<div class="alert alert-danger text-center col-lg-8 col-lg-offset-2 col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-10 col-xs-offset-1">
                Erro ao Salvar! <br> Tente novamente em alguns segundos <br> caso o erro persista informe seu lider
            </div>
            <div class="col-lg-3 col-lg-offset-2 col-md-3 col-md-offset-2 col-sm-4 col-sm-offset-2 col-xs-4 col-xs-offset-2">
                <div class="btn btn-sm btn-default" onclick="limpar()"><i class="fa fa-fw fa-lg fa-plus-square-o"></i>&nbsp;Novo</div>
            </div>
            <div class="col-lg-3 col-lg-offset-2 col-md-3 col-md-offset-2 col-sm-4 col-sm-offset-1 col-xs-4 col-xs-offset-1">
                <div class="btn btn-sm btn-primary" onclick="guardar()"><i class="fa fa-fw fa-lg fa-save"></i>&nbsp;Salvar</div>
            </div>
            ';
        }

    echo $out;
}


?>

-classifier_HelpCenter_sql.php

<?php 
    set_time_limit( 0 );
    date_default_timezone_set('America/Sao_Paulo');
    include '..\..\complemento\sql_login.php';
    if(!isset($_SESSION)){session_start();}

        function obtiene_Atendimento($Atendimento){
        $conexion = conexion();
        $link_augusta = sqlsrv_connect( $conexion[ "idt_augusta" ][ "server" ], $conexion[ "idt_augusta" ][ "info" ] );

        $sql= "SELECT T.Atendimento
            FROM classificadorHelpCenter_atendimento AS T";

        $resultado = sqlsrv_query( $link_augusta, $sql );
        while( $fila = sqlsrv_fetch_array( $resultado, SQLSRV_FETCH_ASSOC ) ) {
            $arreglo =  utf8_encode( $fila[ "Atendimento" ] );
        }

        sqlsrv_free_stmt( $resultado );
        sqlsrv_close( $link_augusta ); 
        return @$arreglo;

    }
    function obtiene_filtros ($filtro,$aux){
        $conexion = conexion();
        $link_augusta = sqlsrv_connect( $conexion[ "idt_augusta" ][ "server" ], $conexion[ "idt_augusta" ][ "info" ] );

        if($filtro=='Solicitante'){
            $sql = "SELECT A.SOLICITANTE AS valor
                FROM classificadorHelpCenter_solicitante AS A
                GROUP BY A.SOLICITANTE ORDER BY A.SOLICITANTE";
        }
        if($filtro=='subSolicitante'){
            $sql = "SELECT A.SUBSOLICITANTE AS valor
            FROM classificadorHelpCenter_solicitante AS A
            WHERE A.SOLICITANTE = '".$aux."'
            GROUP BY A.SUBSOLICITANTE
            ORDER BY A.SUBSOLICITANTE";
        }

        $resultado = sqlsrv_query( $link_augusta, $sql );
        while( $fila = sqlsrv_fetch_array( $resultado, SQLSRV_FETCH_ASSOC ) ) {
            $arreglo[] = array( 
                "valor"     => utf8_encode( $fila[ "valor" ] ),
            );
        }

        sqlsrv_free_stmt( $resultado );
        sqlsrv_close( $link_augusta ); 
        return $arreglo;
    }   
    function guardarResposta($Atendimento, $Solicitante, $SubSolicitante){
        $conexion = conexion();
        $link_augusta = sqlsrv_connect( $conexion[ "idt_augusta" ][ "server" ], $conexion[ "idt_augusta" ][ "info" ] );

        $sql="INSERT INTO fale_classificadorHelpCenter_registros (nome, bp, lider, Solicitante, Subsolicitante, data, Atendimento)
        VALUES( '".$_SESSION['nomeGuerra']."', '".$_SESSION['bp']."', '".$_SESSION['contrato_resumido']."', '".$Solicitante."', '".$SubSolicitante."', '".date('Y-m-d H:i:s')."' ,'".$Atendimento."')";

        $arreglo = sqlsrv_query( $link_augusta, $sql );
        sqlsrv_close( $link_augusta ); 
        return $arreglo;
    }


?>

Everything related to Support, I tried to use the same setting as the Requester field. The Answer field appears in the form, but when I click, it does not bring the result of the sql query I did.

I need to make the list appear.

Thanks for your help.

  • Dude.. looking up I could see HTML comments inside JS. <script><!-- carregar form --></script> is not valid JS. Maybe it has to do with your problem.

1 answer

0


Hello, you should note the return of the call to service classifiers_HelpCenter_ajax in the browser console / open the Network tab search in Name => classifier_HelpCenter_ajax and click on it right side has Preview and Response, you click on them you must see what is returning to the html page in case you have to see if it is returning Support. I hope I’ve helped.

Browser other questions tagged

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