im trying to make a comment system what im trying to do is to send a comment to a post and will pop up right away after sending but i dont know exactly how to do it.. what i did was when i send a comment i use window.location.reload
to pop up my comment
<div id="mycomments">
<?php
foreach ($post_model->getcomment() as $value) {
if($postid == $value['post_uid']){
?>
<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>
<?php
}
}
?>
</div>
with this code i can show my comment and with sending the comment i use this.
$(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.");
}
});
});
this is where i send my request.
post_controller.php
<?php
if(session_id() == '' || !isset($_SESSION))
session_start();
$session_id = $_SESSION['id'];
require_once('../model/dbconfig.php');
require_once('../model/post.php');
$database = new Database();
$conn = $database->getConnection();
$com = new post($conn);
$comments = ($_POST['comments']);
$uid = ($_POST['id']);
if(isset($_POST['id']) && isset($_POST['comments'])){
$res = $com->comment($uid,$session_id,$comments);
}
?>
what i want to do is when i send a comment it will show right after i send the comment and it will show like this
i tried using $("#mycomments").append(data);
but what happen is it only display the query of my db to insert the comment.. but not the html format.