5
Updated 5
Good morning guys, I’m trying to create a comment system like http://demo.hazzardweb.net/comments/
below we have the javascript code that sends the form and then adds the comment, it is almost all right, missing modify something, because I have many forms with such id, as it does to send each form with each id?
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 the values to 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 send the 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>
Following what Anderson Nunes said is just you use Json, basically you will commit html to php using Jquery and make the comment go to php and the same gives a return to html and you populate an html element without giving refresh.
– Maicon e Nanda Blumenau
I edited all your question. Study the issue so as not to make the same mistakes in future questions, keep them well formatted.
– Rafael Almeida
Kevin, I have removed the excess bold and citations from your question, see Help Center and this thread http://meta.pt.stackoverflow.com/questions/1084/howwe shouldformatt-questions-answers how to format your questions and answers.
– gustavox
Thanks guys, for correcting me, I’m adapting to stackoverflow :)
– Kevin mtk
I just think it would be interesting for you to keep the original question, the person enters the question, and sees in your answer the answer you adopted and does not understand the scope of your question...
– MarceloBoni
One of the features you can(and should!) use is to select the best answer
– MarceloBoni
sorry had forgotten to select, and I think the person who sees the question and understands the least of php, or at least has will, will search on how to send form with ajax, only this, because the question was practically based on it.
– Kevin mtk
I reversed the last issue because you had removed the most important: the question! I recommend a [help] to better understand how the site works. The questions are not like a chat with experts, with many shuttles between the interlocutors. The format is direct questions with direct answers. Always try to divide your problem into smaller problems, and ask separately about each of them. So the content is more likely to be useful to people other than you. Keep turning the question while you’re fiddling with the code doesn’t work well here.
– bfavaretto