Inquiry $ .get json and show result

0

I am sending an array to a query in a MySQL database via $ .getJSON and in php I am putting a table, but I am not able to return and display this table, how can I do that?

My code that sends the parameters looks like this:

         // Gravação e mensagem no retorno do cadastro do comentário
    $(document).ready(function () {
        $("#btnImprimeLista").click(function () {

            var nChecked = new Array();

            $("input[name='check[]']:checked").each(function () {
                nChecked.push(parseInt($(this).val()));
            });

            $.getJSON(
                'imprimetodoslista.php', {
                    list: nChecked
                },
                function (json) {
                console.log(json);
                // Mostrar a tabela no retorno      
                }
            );
        });
    }); 

PHP looks like this:

// Consultando candidados no banco
foreach($checkboxes as $id) {

    mysql_select_db($database_con, $con);
    $query_rsRegistro = "SELECT nome, email, celular, id_municipio, id_uf, dt_nascimento FROM candidato WHERE id_candidato = '$id'";
    $rsRegistro = mysql_query($query_rsRegistro, $con) or die(mysql_error());
    $row_rsRegistro = mysql_fetch_assoc($rsRegistro);
    $totalRows_rsRegistro = mysql_num_rows($rsRegistro);

    $Nome = $row_rsRegistro['nome'];
    $Email = $row_rsRegistro['email'];
    $Celular = $row_rsRegistro['celular'];
    $Municipio = $row_rsRegistro['id_municipio'];
    $UF = $row_rsRegistro['id_uf'];
    $Nascimento = $row_rsRegistro['dt_nascimento'];

    $tabela = "     
     <table width=100%  border=0 cellpadding=2 cellspacing=1>
      <tr>
        <td>Nome</td>
        <td>E-mail</td>
        <td>Celular</td>
        <td>Municipio</td>
        <td>UF</td>
        <td>Nascimento</td>
      </tr>";            
    do {            
      $tabela .= "
      <tr>
        <td>{$Nome}; </td>
        <td>{$Email}; </td>
        <td>{$Celular}; </td>
        <td>{$Municipio};</td>
        <td>{$UF};</td>
        <td>{$Nascimento};</td>     
      </tr>";
    } while ($row_rsRegistro = mysql_fetch_assoc($rsRegistro)); 
    $tabela .= "</table>";  

}

$arr['msg'] = $tabela;      

$arr = array_map('htmlentities',$arr);
echo json_encode($arr); 
    
asked by anonymous 01.07.2014 / 18:31

1 answer

1

The problem is in the return of the code, you are using the getJSON function, in this case the PHP value should be returned via JSON and not as HTML.

In the way you're using, change the getJSON function to $ .get ( link ) or return values in JSON without HTML passing only the results / array ( link ) so that JavaScript can interpret the return.

json_encode($row_rsRegistro);

I hope I have helped you.

    
01.07.2014 / 23:01