Updated 5
Good morning, I'm trying to create a comment system that is equal to link
>
Here is the JavaScript code to send everything to PHP:
$(function() {
$('.commentform').submit(function() {
var comment_publication_id = $(this).find('input[name=comment_publication_id]').val();
var comment = $(this).find('textarea[name=comment]').val();
var dataString = 'comment=' + comment + '&comment_publication_id=' + comment_publication_id;
var divtoload = '#commentsforpublication' + comment_publication_id;
var sendcommentbutton = $(this).find('button[type=submit]');
var commenttextarea = $(this).find('textarea[name=comment]');
if (comment == ""){
alert("Não pode deixar em branco!");
} else
$.ajax({
type: "POST",
url: "sendcomment.php",
data: dataString,
dataType: 'json',
cache: false,
success: function(mydata) {
$(sendcommentbutton).attr("disabled", true);
$(commenttextarea).attr("disabled", true);
$("#" + comment_publication_id + ".commentform").prepend('<div id="loading"><img src="/img/ajax-loader.gif" align="absmiddle"></div>');
$("textarea#commentto" + comment_publication_id).val('');
setTimeout(function(){
$("#loading").remove();
var addcomment = '<div> "Exemplo, coloca mydata.nome do array que está no php " </div>';
$(divtoload).append(addcomment);
$(divtoload).find(".commentbox:last").hide().fadeIn('slow').slideDown("normal");
$(sendcommentbutton).attr("disabled", false);
$(commenttextarea).attr("disabled", false);
}, 4000);
}
});
return false;
});
});
sendcomment.php (Here I do the INSERT and return values for JavaScript):
mysqli_query($conexao,"INSERT INTO questioncomments (comment_question_id,comment_autor_id,comment,comment_datetime) VALUES ('$comment_question_id','$comment_autor_id','$comment',NOW())");
$last_insert_id = mysqli_insert_id($conexao);
$questioncomments = "SELECT questioncomments.*, login.* FROM questioncomments INNER JOIN login ON questioncomments.comment_autor_id = login.user_id WHERE comment_id = $last_insert_id LIMIT 1";
$commentsresult = $conexao->query($questioncomments);
while ($rowcomments = $commentsresult->fetch_assoc()) {
$nome = $rowcomments["Nome"];
$comment_question_id = $rowcomments["comment_question_id"];
$commentdatetime = date('d/m/Y \à\s H:i', strtotime($rowcomments["comment_datetime"]));
}
// array
$my = array(
'comment_id'=>$last_insert_id,
'user_id'=>$comment_autor_id,
'Nome'=>$nome,
'comment_date_time'=>$commentdatetime
);
// converto ele com a função json_encode
$myJSON = json_encode($my);
// coloco na tela o objeto javascript
echo($myJSON);
Form to submit comment:
<form method="post" class="commentform" id=" (Aqui é a ID da publicação) ">
<input type="text" name="comment" id="comment" class="sendcomment">
<input type="hidden" name="comment_question_id" id="comment_question_id" value=" (Aqui é a ID da publicação) ">
<button type="submit" class="sendcomment-button">Enviar comentário</button>
</form>