How to show table in return JSON?

Asked

Viewed 928 times

4

Accurate, when returning from an insert pass parameter to a page and load it into a div specific, I tried something like this:

var formData = new FormData(this);
$.ajax({
    url: formURL,
    type: 'POST',
    data: formData,
    mimeType: "multipart/form-data",
    contentType: false,
    cache: false,
    processData: false,
    success: function(data, textStatus, jqXHR) {

     var json = $.parseJSON(data);
     var IdContrato = json.par;

    // TABELA COM DOCUMENTOS DO CONTRATO
    $("#resultado-upload").load('pListaUpload.php',{IdContrato:IdContrato});
    // LIMPANDO CAMPOS DO FORMULÁRIO
    $("#sObservacao").val("");          
    },
    error: function(jqXHR, textStatus, errorThrown) {
        $("#resultado-upload").html('<pre><code class="prettyprint">REQUISIÇÃO AJAX FALHOU<br/> textStatus=' + textStatus + ', errorThrown=' + errorThrown + '</code></pre>');
    }
});
e.preventDefault();
e.unbind();

The page php which searches the data and should be shown is this:

// Array que irá retornar o status da execução do script (JSON)
$aretorno = array();    
$aretorno["msg"] = "";
$aretorno["status"] = "";

@$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

// Checar conexão
if ($conn->connect_errno) {
    $aretorno["msg"] = "Conexão ao banco de dados falhou.";
    $aretorno["status"] = "ERRO";
    echo $aretorno;
    exit;
}

if (!$conn->set_charset("utf8")) {
    $aretorno = "Erro ao carregar conjunto de caracteres utf8.";
    echo $aretorno;
    exit;
}

$sql = "SELECT * FROM gerDoctoContrato WHERE IdContrato = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param(
    "i",
    $_POST["IdContrato"]
);

if($stmt->execute()){
    $data = array();    
    $result = $stmt->get_result();
    echo '';
    echo '
            
                Arquivo
                Tipo
                Caminho
                Ação
              
            ';
    echo '';
    while ($row = $result->fetch_assoc()) {

        $IdDocumento = $row['IdDocumento'];
        $IdContrato = $row['IdContrato'];       

        echo '';
            echo '' . $row['NomeArquivo'] .'';
            echo '' . $row['TipoArquivo'] .'';
            echo '' . $row['CaminhoAnexo'] .'';
            echo '';

        echo '';
    }
    echo '';
    echo '';
    $stmt->close();
}else{
    echo "Ocorreu um erro na listagem dos dados: " . $stmt->error . ". Verifique.";
}

//close the database
$conn->close();

And on my return it appears Array, the parameter was able to recover but display the table not.

  • you already have the value of Idcontrato ? and just are not able to load the table, this ?

  • Hi @Thiago Friedman, that’s right.

1 answer

2


Try this, it’s to work if the name of the div is correct and the page too:

$("#resultado-upload").load('pListaUpload.php',{'IdContrato' : IdContrato});

Maybe if it doesn’t work try to make ajax loading this way:

  var value = IdContrato;  
    $.ajax({
      url: "pListaUpload.php",
      data: "key="+value,
      type: "post",
      success: function(data){
            $('#resultado-upload').html(data);
      }
    });
  • Hello @Thiago Friedman, unfortunately in none of the situations worked.

  • strange heim, your ajax really is returning Success ? or always falls into error ?

  • Hello @Thiago, when I use this code: $("#result-upload"). html('<pre><code>'+data+'</code></pre>'); the result that appears is: {"msg":"Record inserted successfully." ,"par":"34","status":""}

  • 1

    Hello @Thiago Friedman Thanks for the help, I redid the code again and now everything is working perfectly.

Browser other questions tagged

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