3
I’m trying to make a comment portal, and so will have a text input in each POST for people to comment, these POSTS will be on the same page (Facebook style), as well as inputs, which will have to be dynamic.
I did this function using Jquery, but it doesn’t work, when sending just start the page again. Someone gives me a light?
HTML:
$(document).ready(function() {
function enviarcomentario(id) {
var iconCarregando = $('<img src="loading.gif" width="70" /> <span class="destaque">Carregando. Por favor aguarde...</span>');
$('#formcomentario' + id).submit(function(e) {
if ($("#texto" + id).val() === '') {
$('#texto' + id).select();
return false;
} else {
e.preventDefault();
var serializeDados = $('#formcomentario' + id).serialize();
$.ajax({
url: 'acoes.php?enviarcomentario=1',
dataType: 'html',
type: 'POST',
data: serializeDados,
beforeSend: function() {
$('#statuscomentario' + id).html(iconCarregando);
$('#send' + id).attr('disabled', 'disabled'); //desabilito
},
complete: function() {
$(iconCarregando).remove();
},
success: function(data, textStatus) {
$('#send' + id).removeAttr('disabled'); //habilito
$('#texto' + id).val("");
},
error: function(xhr, er) {
$('#formcomentario' + id).html('<p class="destaque">Error ' + xhr.status + ' - ' + xhr.statusText + '<br />Tipo de erro: ' + er + '</p>');
}
});
}
}
);
}
});
<form action="" method="POST" id="formcomentario<?php echo $row['idd']; ?>">
<img class="img-responsive img-circle img-sm" src="<?php if($user['imagem']) { ?>fotos/<?php echo $user['imagem']; } else { ?> estilos/img/sem-foto.png<?php } ?>" alt="Alt Text">
<!-- .img-push is used to add margin to elements next to floating images -->
<div class="img-push"><input type="hidden" name="idp" value="<?php echo $row['idd']; ?>">
<input type="text" class="form-control input-sm" id="texto<?php echo $row['idd']; ?>" name="texto" placeholder="Escreva um comentário">
<input type="submit" onclick="enviarcomentario(id)" style="display: none;">
<div id="statuscomentario<?php echo $row['idd']; ?>"></div>
</div>
</form>
at what point you are executing the function:
enviarcomentario(id)
?– Jader A. Wagner
<input type="Submit" onclick="send comment(id)" style="display: None;"> I’ve tried that too and won’t
– Vinicius Henzel
you need to add the eventlistner
submit(
before clicking and sending... remove it from that function and find another way to get the ID you need, such as a hidden field or date or action attribute of the form.– Jader A. Wagner
I don’t know how to do it, I’ve already searched the whole Google, and I don’t understand how
– Vinicius Henzel