I'm trying to make a comment portal, so I'll have a text input on every POST for people to comment, these POSTS will be on the same page (Facebook style), as well as the inputs, which will have to be dynamic.
I made this function using Jquery, but it does not work, when sending, it only starts the page again. Somebody give 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>