Does not show all records when performing an SQL SERVER query through AJAX

6

I have a problem getting all the records of a query in SQL SERVER through PHP and AJAX.

When I send POST by the classic method of the form (submit), all records are displayed without any problem.

However, when doing the query by means of the AJAX method, it DOES NOT visualize the 5 registers, only 4, but strangely the fourth record is incomplete, it does not even finish generating the

BD connection file (connection_consulta.php):

$cnx=mssql_connect("CRONOS\SQLEXPRESS","consulta","12347"); mssql_select_db("BD_TRAMITE",$cnx);

File query.php with the code jQuery and AJAX:

$(document).on("click","#img_Buscar_tramite",function(){
    consulta_tramite();
});

function consulta_tramite()
{
    $('#dv_Contenido').html("<h4>El proceso tardará varios munitos.</h4> <br> <img src='img/cargar.gif'>");
    var codigo_tramite = $('#txt_Valor').val().trim();
    var anio_actual = $('#hdd_Anio_actual').val();

    var cod_tramite = codigo_tramite+'-'+anio_actual;

    $.ajax({
      type: 'POST',
      dataType: 'json',
      url: 'ajax_consulta.php',       
      data: {op:1, cod_tramite:cod_tramite},
   }).done(function(respuesta){
        $('#dv_Contenido').html(respuesta.contenido);
        //$('#sp_Cod_tramite').html(cod_tramite);
   }).fail(function(jqXHR, textStatus, errorThrown, respuesta){
        // Log the error usuarios_online.php
        console.log('The following error occurred: '+ textStatus + ' - ' + jqXHR.responseText + ' - ' + errorThrown);
        $('#dv_Contenido').html('The following error occurred: '+ textStatus + ' - ' + jqXHR.responseText + ' - ' + errorThrown);
    }); 
}

File ajax_consulta.php:

class C_process {

public $contenido = '';

function consulta_tramite()
{
    require_once("../conexion/conexion_consulta.php");
    $cod_tramite = $_POST['cod_tramite'];

    $sql="SELECT iCodTramite,
                cCodificacion,
                fFecRegistro, 
                cDescTipoDoc, 
                cNroDocumento, 
                cAsunto, 
                nFlgEstado, 
                nNumFolio, 
                cReferencia, 
                cObservaciones, 
                nTiempoRespuesta
            FROM Tra_M_Tramite t
            INNER JOIN Tra_M_Tipo_Documento d ON d.cCodTipoDoc = t.cCodTipoDoc
            WHERE cCodificacion ='".$cod_tramite."'";
    $result=mssql_query($sql,$cnx);
    $canr = mssql_num_rows($result);

    if(empty($canr))
    {
        $this->contenido = "<font color='red'><b>Ingrese correctamente el codigo del tramite.</b></font>";
        return;
    }

    $RsTra=MsSQL_fetch_array($result);


    $this->contenido = "<table border='1' width='100%'>
                <tr style='background: #688d40; color: #fff;'>
                    <td style='border: 2px solid #fff;'>Fecha</td>
                    <td style='border: 2px solid #fff;'>Asunto</td>
                    <!--<td style='border: 2px solid #fff;'>Observaciones</td>-->
                    <td style='border: 2px solid #fff;'>Destino</td>
                    <!--<td style='border: 2px solid #fff;'>Responsable / Fecha de Aceptado</td>-->
                    <td style='border: 2px solid #fff;'>Estado</td>
                </tr>";

                $sqlM="SELECT * FROM Tra_M_Tramite_Movimientos WHERE (iCodTramite='".$RsTra['iCodTramite']."' OR iCodTramiteRel='".$RsTra['iCodTramite']."') AND (cFlgTipoMovimiento=1 OR cFlgTipoMovimiento=3 OR cFlgTipoMovimiento=5) ORDER BY iCodMovimiento ASC";
                $rsM=mssql_query($sqlM,$cnx);
                $cant =mssql_num_rows($rsM);
                $contaMov=0;

                $this->contenido .= "<tr><td colspan=4>Cant: ".$cant."</td></tr>";
                while ($RsM=MsSQL_fetch_array($rsM))
                {
                    $this->contenido .= "
                    <tr>
                        <td style='width: 70px;'>".date('d-m-Y', strtotime($RsM['fFecDerivar']))."</td>
                        <td>";

                            $this->contenido .= $RsM['cAsuntoDerivar'];
                        $this->contenido .= "
                         </td>

                        <td>";
                                if($RsM['nFlgTipoDoc']!=3){
                                    $sqlOfiD="SELECT * FROM Tra_M_Oficinas WHERE iCodOficina='".$RsM['iCodOficinaDerivar']."'";
                                    $rsOfiD=mssql_query($sqlOfiD,$cnx);
                                    $RsOfiD=mssql_fetch_array($rsOfiD);
                                    $this->contenido .= trim($RsOfiD['cNomOficina']);
                                }else{
                                    $codi=trim($RsM['cReferenciaDerivar']);
                                    $sqlEstado="SELECT * FROM Tra_M_Doc_Salidas_Multiples WHERE cCodificacion='".$codi."' AND fRecibido is not null ";

                                    //echo $sqlEstado;
                                    $rsEst=mssql_query($sqlEstado,$cnx);
                                    $RsEstado=mssql_fetch_array($rsEst);
                                    if($RsEstado['fRecibido']!=""){
                                        if($RsEstado['cFlgEstado']==1 or $RsEstado['cFlgEstado']==2){
                                                $this->contenido .= "<font size=\"0.5\">Comunicacion Atendida</font>";
                                        }else {
                                                $this->contenido .= "<font size=\"0.5\">Comunicacion En Proceso</font>";
                                        }
                                    }else{
                                            $this->contenido .= "<font size=\"0.5\">En Comunicacion</font>";
                                    }
                                }
                        $this->contenido .= "
                        </td>       

                        <td>";
                                if($RsM['cFlgTipoMovimiento']!=5){  
                                    if($RsM['fFecRecepcion']!=""){
                                        switch ($RsM['nEstadoMovimiento']) 
                                        {
                                            case 1:
                                                $this->contenido .= "En Proceso";
                                            break;
                                            case 2:
                                                $this->contenido .= "Derivado";
                                            break;
                                            case 3:
                                                $this->contenido .= "Asignado";
                                                break;
                                                case 4:
                                                $this->contenido .= "Respondido";
                                            break;
                                            case 5:
                                                $this->contenido .= "Finalizado";
                                                $this->contenido .= "<div style=color:#0154AF;font-size:10px>".$RsM['cObservacionesFinalizar']."</div>";
                                            break;
                                        }
                                    }else{
                                            $this->contenido .= "Pendiente";
                                    }
                                }else{
                                        $this->contenido .= "";
                                }
                                if($RsM['iCodTrabajadorDelegado']!=""){
                                    $rsDelg=mssql_query("SELECT * FROM Tra_M_Trabajadores WHERE iCodTrabajador='".$RsM['iCodTrabajadorDelegado']."'",$cnx);
                            $RsDelg=MsSQL_fetch_array($rsDelg);
                            $this->contenido .= "<div style=color:#005B2E;font-size:12px>".$RsDelg["cApellidosTrabajador"]." ".$RsDelg["cNombresTrabajador"]."</div>";
                                        mssql_free_result($rsDelg);
                                    }
                        $this->contenido .= "
                        </td>
                    </tr>";
                    $contaMov++;
                }
}

}

$respuesta = new C_process();

$respuesta->consulta_tramite();

echo json_encode($respuesta);

I await your contributions because I need to solve this problem. Greetings. Thank you.

    
asked by Jose Freyre 02.10.2018 в 01:07
source

2 answers

0

For your ajax you must specify the data type in the "dataType" tag and you have specified "json" but you are returning an "html" table so your ajax should look like this:

$.ajax({ContentType : 'application/x-www-form-urlencoded; charset=UTF-8',
        dataType: 'html',
        type : 'POST',
        url: 'ajax_consulta.php',     
        data: {op:1, cod_tramite:cod_tramite}
      })...

I think you have some kind of problem with the encode "UTF-8", use the strings in this way to avoid problems of tildes and the Ñ that is where your query stops (If that is the case).

 $this->contenido .= utf8_encode($RsM['cAsuntoDerivar']);

And in your file ajax_consulta.php you must include header( 'content-type: text/html; charset=utf-8' ); what is your ajax expected to receive; Finally I do not see in what moment you close the table with the </table> tag

I recommend this link to answer questions

Greetings

    
answered by 02.10.2018 в 02:24
0

Thanks for your answers. And the truth is that they are all right.

Everything has to do with the special characters (ñ, tildes, etc). And the solution was to use the function utf8_encode Example: utf8_encode($RsM['cAsuntoDerivar'])

Solved. Thanks Mundo. Greetings.

    
answered by 02.10.2018 в 17:48