Lion Smith
Lion Smith

Reputation: 655

display comment in post comment section without reloading the page

im trying to make a facebook style post and comment section but i dont know how to display the data inserted to my database without refreshing the page...

this code is for saving the data to my db. i use window.location.reload(); to reload my page so that the data will be displayed on my page..

<script>
$(document).ready(function() {
$('input[name="mycomment"]').on('keyup', function(e){
e.preventDefault();
var comments = $(this).val();
var sid = $(this).closest("div#userspost").find("input[type='hidden']").val();
if(e.keyCode == 13){        
if(comments.length)
$.ajax({
url: "../controller/post_controller.php",
type: "POST",
data:{ 
"id":sid,
"comments":comments,
},
success: function(data)
{

window.location.reload();
}
});
else
alert("Please write something in comment.");
}
});
}); 
</script>

using this script i can display my comment on a post i need to refresh the page first for me to be able to show the comment.

<?php 

foreach ($post_model->getcomment() as $value) {
if($postid == $value['post_uid']){

?>
<div id="mycomments">
<div class="col-lg-12" style="background:#eff9c7;">
<img src="./<?php echo $value['image']?>" class="pull-left" style="border-radius:50%;margin-top:10px;" width="7%" height="7%" />
<p style="margin-top:18px;line-height:15px;"><strong class="font-1" style="margin-left:10px;"><?php echo $value['firstname'].' '.$value['lastname']?></strong> <?php echo $value['pc_comment']?><br>
<span class="" style="margin-left:10px;font-size:.9em;color:gray;"><abbr class="timeago" title="<?php echo $value['pc_datesend']?>"></abbr></span> 
</p>
</div>
</div>
<?php
}
}
?>

what im trying to do is that this is where i want to display my comments from my db. i tried researching about append/load but i dont exactly know how this works. is there any idea that i can display my comment in this script?

<div id="mycomments">
<div class="col-lg-12" style="background:#eff9c7;">
<img src="./<?php echo $value['image']?>" class="pull-left" style="border-radius:50%;margin-top:10px;" width="7%" height="7%" />
<p style="margin-top:18px;line-height:15px;"><strong class="font-1" style="margin-left:10px;"><?php echo $value['firstname'].' '.$value['lastname']?></strong> <?php echo $value['pc_comment']?><br>
<span class="" style="margin-left:10px;font-size:.9em;color:gray;"><abbr class="timeago" title="<?php echo $value['pc_datesend']?>"></abbr></span> 
</p>
</div>
</div>

Upvotes: 0

Views: 2204

Answers (1)

Neil
Neil

Reputation: 14313

I decided to write some pseudo code for you here. If you don't know how to store and fetch comments I would recommend looking into MYSQL. It's relatively simple (for simple things), so that shouldn't be too big of a problem. YouTube tutorials will be your blessing there.

You should have at least three files to properly implement this:

uploadComment.php

<?php

//process the comment upload
echo $_POST['comment'];

?>

getComment.php

<?php

//however you serve commnets, MYSQL, maybe?
//make sure it's properly formatted with HTML

?>

index.html

<form>
    <input type="text" name="comment" id="comment" value="Comment here" />
    <button id="submit">Submit</button>
</form>

<div id="comments">

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>

$("#submit").click(function () {
    $.post("uploadComment.php", {
        comment : $("#comment").val()
    }, function (data) {
        //comment posted.
        refreshComments();
    });
});

function refreshComments() {
    $.get("getComments.php", function(data) {
        $("#comments").html(data);
    });
}

setInterval(refreshComments,5000);


</script>

Note: Although it may be annoying, if you want immediate satisfaction, append the new comment to the end and then invoke refreshComments. (But I wouldn't recommend doing this because it would force you to update multiple locations in your code whenever you change your comment HTML format).

Upvotes: 1

Related Questions