Save Text Field Record Disabled AJAX + PHP

Asked

Viewed 63 times

0

Friends, good morning!

I have a form in php where I have to perform maintenance, but it was not me who developed, the former analyst is no longer in the company.

In this form, there is a field where the person enters a locator code (company), and has a field next to it that is disabled, where it brings the company name. In the form records, only the locator code is saved, and the need to save in a column next to, the company name as well. However, I am not able to make the SQL Insert save this information in the record, when saved, the column that should bring the name, comes with the blank field.

In PHP codes, code related fields are referred to as "OAC" and the ICAO name is the part where I can’t get you to stay safe on the record.

Below the PHP index code of the form:

<html>
<head>
    <title>Augusta | Classificador E-LATAM</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_Nucleo_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_Setor').value = "";
                        document.getElementById('filtro_Tipo').value = "";
                    }
                })
            }           
        }
    </script>
    <script> <!-- carrega sutipo -->
        function carregarSubtipo() {
            if( true ) {
                $.ajax({
                    url: "classificador_Nucleo_ajax.php", 
                    dataType: "html",
                    type: "POST", 
                    data: { 
                        'seccion': 'carregarSubtipo',
                        'tipo'  : $('#filtro_Tipo').val()
                    }
                    ,beforeSend: function(){
                        $("#div_subtipo").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_subtipo").html( data ); 
                        document.getElementById('filtro_Subtipo').value = "";
                    }
                })
            }           
        }
        function carregarOAC() {
            if( true ) {
                $.ajax({
                    url: "classificador_Nucleo_ajax.php", 
                    dataType: "html",
                    type: "POST", 
                    data: { 
                        'seccion': 'carregarOAC',
                        'oac'   : $('#filtro_OAC').val()
                    }
                    ,beforeSend: function(){
                        $("#div_OAC_nome").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_OAC_nome").html( data ); 
                    }
                })
            }
        }
    </script>
    <script> <!-- function guardar e limpar -->
        function guardar(){
            if (  $( "#filtro_Setor" ).val() == null){
                alert("Por favor, informe o Setor");
                $("#filtro_Atendimento").focus();
                return false;
            }
            if (  $( "#filtro_Tipo" ).val() == null){
                alert("Por favor, informe o tipo da ligação");
                $("#filtro_Tipo").focus();
                return false;
            }
            if (  $( "#filtro_Subtipo" ).val() == null){
                alert("Por favor, informe o subtipo da ligação");
                $("#filtro_Subtipo").focus();
                return false;
            }
            if (  $( "#filtro_OAC" ).val() == null){
                alert("Por favor, informe o código OAC");
                $("#filtro_OAC").focus();
                return false;
            }
            var valor  = document.getElementById("filtro_OAC").value;
            if (valor.length != 5) {
                alert("Por favor, informe um código OAC válido");
                $("#filtro_OAC").focus();
                return false;
            }
            if(true) {
                $.ajax({
                    url: "classificador_Nucleo_ajax.php", 
                    dataType: "html",
                    type: "POST", 
                    data: { 
                        'seccion'   : 'guardar',
                        'Setor'     : $( "#filtro_Setor").val(),
                        'tipo'              : $( "#filtro_Tipo" ).val(),
                        'subtipo'           : $( "#filtro_Subtipo").val(),
                        'oac'   : $( "#filtro_OAC").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('Nucleo', 'Classif', @$_SESSION[ "bp" ])?><![endif]-->
    <!--[if ! IE]><!--><?php imprimeMenuHeaderAugusta('Nucleo', 'Classif', @$_SESSION[ "bp" ]) ?><!--<![endif]-->

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

</body>
</html>

Below the code of the AJAX part of php:

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

    $seccion = $_POST["seccion"];
    @$setor = $_POST['Setor'];
    @$tipo = $_POST['tipo'];
    @$subtipo = $_POST['subtipo'];
    @$oac = $_POST['oac'];

    if($seccion == "carregarForm"){ carregarForm();}
    if($seccion == "carregarSubtipo"){ carregarSubtipo($tipo);}
    if($seccion == "guardar"){ guardar($setor, $tipo, $subtipo, $oac);}
    if($seccion == "carregarOAC"){ carregarOAC($oac);}

function carregarForm(){

    $tipos = obtiene_filtros('tipo', null);
    $filtro_Setor = obtiene_Setor();

    $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 E-LATAM</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-7 col-md-7 col-sm-7 col-xs-7" style="padding-left:0px;">
                                        <input type="text" class="form-control" id="filtro_Nome" disabled value="'.$_SESSION['nomeGuerra'].'">
                                    </div>
                                </div>';
                            }
                            if(true){//Setor(Tipo Ligação)
                            $out.='<fieldset>
                                <div class="form-group">
                                    <label for="filtro_Setor" class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label">Tipo Ligação</label>
                                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" style="padding-left:0px;">
                                        <select class="form-control" id="filtro_Setor">';
                                            foreach ($filtro_Setor as $a){
                                                $out.='<option value="'.utf8_decode($a['setor']).'">'.utf8_decode($a['setor']).'</option>';
                                            }
                                        $out.='</select>
                                    </div>
                                </div>
                                </fieldset>';
                            }
                            if(true){//tipo
                                $out.='<div class="form-group">
                                    <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label" for="filtro_Tipo">Motivo</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_Tipo" onchange="carregarSubtipo();">';
                                            foreach ($tipos as $v){
                                                $out.='<option value="'.utf8_decode($v['valor']).'">'.utf8_decode($v['valor']).'</option>';
                                            }
                                        $out.='</select>
                                    </div>
                                </div>';
                            }
                            if(true){ //subtipo
                                $out.='<div class="form-group" id="div_subtipo">
                                    <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label" for="filtro_Subtipo">Submotivo</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_Subtipo"></select>
                                    </div>
                                </div>';
                            }
                            if(true){ //OAC
                                $out.='<div class="form-group">
                                    <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label" for="filtro_OAC">OAC</label>
                                    <div class="col-lg-3 col-md-3 col-sm-9 col-xs-9" style="padding-left:0px;">
                                        <input type="text" class="form-control" id="filtro_OAC" >
                                    </div>
                                    <div class="col-lg-6 col-md-6 col-sm-9 col-xs-9" style="padding-left:0px;" id="div_OAC_nome">
                                        <input type="text" class="form-control" id="filtro_OAC_nome" disabled >
                                    </div>
                                </div>
                                <div class="form-group hidden" id="div_alertOAC">
                                    <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label"></label>
                                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-9 alert-sm alert-danger">
                                        O OAC deve ser um código de 5 digitos.
                                    </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>';

            /*Valida se o OAC tem 5 digitos*/
            if(true){
            $out.='<script>
                    filtro_OAC.onblur = function() {
                        var valor  = document.getElementById("filtro_OAC").value;
                        if (valor.length != 5) {
                            document.getElementById("div_alertOAC").classList.remove("hidden");
                        } else {
                            if (valor.length >= 1) {
                                document.getElementById("div_alertOAC").classList.add("hidden");
                                carregarOAC();
                            }
                        }
                    };
                </script>';
            }
echo $out;
}
function carregarSubtipo($tipo){

    $filtro_duvida = obtiene_filtros('subtipo',utf8_decode($tipo));

    $out = '<label class="col-lg-3 col-md-3 col-sm-3 col-xs-3 control-label" for="filtro_Subtipo">Subtipo</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_Subtipo">';
            foreach ($filtro_duvida as $v){
                $out .= "<option value='".utf8_decode($v['valor'])."'>".utf8_decode($v['valor'])."</option>";
            }
        $out.='</select>
    </div>';
echo $out;
}
function guardar($setor, $tipo, $subtipo, $oac){
        if(guardarResposta(utf8_decode($setor),utf8_decode($tipo),utf8_decode($subtipo),utf8_decode($oac))){
            $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;
}
function carregarOAC($oac){

        $nomeOAC = obtiene_OAC($oac);

        $out = '<input type="text" class="form-control" id="filtro_OAC_nome" disabled value="'.$nomeOAC.'" display="'.$nomeOAC.'">';


        echo $out;
}

?>

The latest Function loadOAC($oac) is where it brings the disabled field with the OAC code name...

And the code below refers to SQL query made to save the records in the SQL database:

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

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

        $sql= "SELECT T.Setor AS Setor
            FROM ClassificadorNucleo_setor AS T";

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

        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=='tipo'){
            $sql = "SELECT A.TIPO AS valor
                FROM ClassificadorNucleo_motivos AS A
                GROUP BY A.TIPO ORDER BY A.TIPO";
        }
        if($filtro=='subtipo'){
            $sql = "SELECT A.SUBTIPO AS valor
            FROM ClassificadorNucleo_motivos AS A
            WHERE A.TIPO = '".$aux."'
            GROUP BY A.SUBTIPO
            ORDER BY A.SUBTIPO ";
        }

        $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($setor, $tipo, $subtipo, $oac){
        $conexion = conexion();
        $link_augusta = sqlsrv_connect( $conexion[ "idt_augusta" ][ "server" ], $conexion[ "idt_augusta" ][ "info" ] );

        $sql="INSERT INTO ClassificadorNucleo_registros (nome, chapa, lider, setor, tipo, subtipo, data, oac)
        SELECT '".$_SESSION['nomeGuerra']."', '".$_SESSION['chapa']."', contrato_resumido, '".$setor."', '".$tipo."', '".$subtipo."', '".date('Y-m-d H:i:s')."', '".$oac."'
        FROM usr_usuarios
        WHERE bp = '".$_SESSION['bp']."'";

        $arreglo = sqlsrv_query( $link_augusta, $sql );
        sqlsrv_close( $link_augusta ); 
        return $arreglo;
    }
    function obtiene_OAC($oac){
        $conexion = conexion();
        $link_augusta = sqlsrv_connect( $conexion[ "idt_augusta" ][ "server" ], $conexion[ "idt_augusta" ][ "info" ] );

        $sql= "SELECT T.NOME
            FROM ClassificadorNucleo_motivosOAC AS T
            WHERE T.OAC = '".$oac."'";

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

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

    }
?>

Follow an image of what the form looks like to make it clearer:

XHTML válido

From now on I thank you for your help!

1 answer

1

Try to change the value $("#filtro_OAC") there in the Ajax to load it. val for $("#filtro_OAC_name"). val

  • Thanks for the reply Ramones, but unfortunately did not resolve. When I change to $("#filtro_OAC_name"). val, and enter the OAC code, the blocked field that brings the name, is empty, does not bring the name. I believe I would have to make some new Function to store the name and code.. But I still haven’t found a way to do this

Browser other questions tagged

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