Clear memory variable data

Asked

Viewed 912 times

0

I have a function that through click detection makes a post to Act.php Act calls a php function that inserts comments.

Everything works, massssssssssssss, as there is no refresh on the page and the intention is the same, when I insert a second comment the code runs, 1 + 2 times, if you insert a third, runs 1 + 3 times, has to "wipe from memory" the data already sent, because it is running several times the same code, only to give refresh on the page.

jQuery:

$("#btnanotacao").click(function() {
    d = new Date;
    idcli = $("#idclientecoment").val();
    anot = $("#fanotacao").val();
    dat = d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate();
    jQuery.ajax({
        type: "POST",
        url: "lib/act.php",
        data: {
            acao: 'coment',
            idclie: idcli,
            anotacao: anot,
            dt: dat
        },
        success: function(data) {
            var a;
            a = parseInt(data);
            if (a != 1) {
                alert(data);
            } else {
                alert("Comentario Inseridos com Sucesso");
            }
        }
    })
    return false;
});

HTML:

<textarea class="form-control" id="fanotacao" rows="6" placeholder="Digite aqui alguma anotação que julgar útil."></textarea>
<input type="hidden" id="idclientecoment" value="<?php echo $id; ?>">
<button type="button" id="btnanotacao" class="btn btn-primary btn-block mt-sm">Save</button>

PHP:

if(isset($_POST['acao']) && $_POST['acao']== 'coment'){
$comentarios        = array(
    'idcli'             => $idclie,
    'data'              => $dt,
    'comentario'        => $anotacao
);
$query = DBCreate('comentarios', $comentarios);
    if($query)
        echo "1";
    else{
        echo "0";
    }
}
  • I think it might be because of anot = $("#fanotacao").val(); you are importing and sending to PHP with the comments you already had. Because you think the code runs more than once?

  • It’s not clear from the code you posted, but from the description of the problem, it looks like you’re adding the "click" event again every time the event fires. If this is the case, you can use: http://api.jquery.com/unbind/, before "scheduling" a new event. Or review your code, so that the event is defined only once, if possible. Check whether the number of "times" the code is running, corresponds to the number of requests sent, or put an "Alert/console.info" inside the click event, and check if the problem is even in Javascript.

  • Yes, in the event click even, already put the Alert, informs 5 times to msg. cannot understand, because there is no loop of repetition, just creates the array, post with jquery, php receives, sends insert in bd, gives the result 1 and Alert is triggered, I will try unbind Aki, I have several identical functions but with form.

  • Yeah, unfortunately it didn’t, this script keeps saving in memory the data sent the first time and making several posts...

  • I discovered the reason for this blessed error, I was calling the file with the js functions twice, in the profile Arq.php when commenting and in the.php annotation document that was called in the .load. I hate grabbing these little things...

No answers

Browser other questions tagged

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