I have a function that through click detection makes a post to act.php act calls a php function that inserts the comments.
Everything works, massssssssssss, as it does not have refresh on the page and the intention is this same, when I insert a second comment the code is executed, 1 + 2 times, if it is to enter a third, it is executed 1 + 3 times, has to "clear from memory" the data already sent, because it is running several times the same code, just to refresh 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";
}
}