Ajax returning repeated data

Asked

Viewed 70 times

0

Whenever I use Ajax to return a database result it stores this result by making a sort of queue in the next requests, returning in other queries old data next to the current ones, example...

$inputgetCod = (isset($_POST['codBarras'])) ? $_POST['codBarras'] : '';
...
else{

    $sql = 'SELECT nome,valorUnt from produtos where codigoBarras = ?';
    $stm = $conexao->prepare($sql);
    $stm->bindValue(1, $inputgetCod);
    $stm->execute();
    $dads = $stm->fetch(PDO::FETCH_OBJ);

    $vlt = $dads->valorUnt;
    $nm = $dads->nome;

    $retorno = array('codigo' => 0, 'mensagem' => 'cadastrado com sucesso', 'acerto' => $acerto, 'nome' => $nm, 'vlt' => $vlt);
            echo json_encode($retorno);
            exit();
}

Ajax

  var ajaxID = 0;
            $.ajax({
            type : 'POST',
            url  : '../phpVld/validaGetprod.php',
            data : data,
            dataType: 'json',
            cache : false,
            success :  function(response){
$(".algumCampo").append(response.algum_dado);
}

when I call this request again via a button it returns the old data together with the current ones, there is some way to return only data from one request at a time?

  • managed to solve the problem?

  • 1

    I got yes man, it was wrong n vote, on the date of the question I did not know much on the site

  • 1

    Normal, even today there is something I doubt

1 answer

0


Change

$(".algumCampo").append(response.algum_dado);

for:

$(".algumCampo").text(response.algum_dado);

append() add more data to the element, while text() will replace with new content

Browser other questions tagged

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