I am making a news feed something like Facebook and other social media platform. For this, I am making a commenting section for each post on the page. I am trying to make the commenting section live (real time), so that when a comment is posted, the page does not refresh.
I know that the commenting system works because I did a test without real time feature (without the use of any form of javascript code).
The following is my code in brief....i only posted what I believe is necessary based on my issue.
function getposts ()
{
global $con;
$getposts = "SELECT * FROM posts";
if ($result = $con->query($getposts)) {
while ($row1 = $result->fetch_assoc()) {
$postid = $row1['post_id'];
?>
<form action='comments_ins.php' method='post' id='reply'>
<input type="hidden" value="<?php echo $postid;?>"
name="postid"/>
<textarea name="comment" id="comment" class="cmt_form"
placeholder="Type a commmment here..."></textarea>
<input type='submit' name='reply' value='Comment'/>
</form>
<?php
}
}
}
?>
Also, the above script i stored in a file given a name of functions. File is php file. The following code is stored in a different file that is named as home where the functions file is included:
<?php include("functions.php"); ?>
<?php getposts ();?>
So, as indicated earlier, the above code works well. Now, I have slightly altered the code to make attempts to have the comment system be real time. The following is the altered code:
function getposts ()
{
global $con;
$getposts = "SELECT * FROM posts";
if ($result = $con->query($getposts)) {
while ($row1 = $result->fetch_assoc()) {
$postid = $row1['post_id'];
?>
<form method='post' id='reply'>
<input type="hidden" value="<?php echo $postid;?>"
name="postid" id="postid" />
<textarea name="comment" id="comment" class="cmt_form"
placeholder="Type a commmment here..."></textarea>
<input type='submit' name='reply' value='Comment'/>
</form>
<?php
}
}
}
?>
//in addition to the java to make it real time
<script type="text/javascript">
$(document).ready(function() {
$(".cmt_form").keypress(function(evt) {
if(evt.which == 13) {
var postid = $("#postid").val();
var body = $("#comment").val();
$.post("comments_ins.php", { postid: postid, comment: body},
function(data) {
$('.log').html(data);
$('#reply')[0].reset();
});
}
});
});
</script>
The above java I included in the same functions.php file but outside of the php tags, not being in any loops. The home file is just the same..no changes.
Finally, the following code is the php file that inserts the comment into the database. File name as seen on previous codes: comments_ins.php
$comment1 = ($_POST['comment']);
$post_id = $_POST['postid'];
global $userId;
$insert1 = "insert into comments (post_id,user_id,comment,date) values
('$post_id','$userId','$comment1',NOW())";
$run1 = mysqli_query($con,$insert1);
The above code works to an extent only:
- it's not posting the correct postid value to the database. It's only posting postid 1 even though i commented on another post with a different id number.
- Also, it's not inputting the comment into the comment field in the database. I see an empty space..no text.
- Finally, the output is some crazy output after posting the comment: some strange numbers and some nonsense.
What have i done wrong?
Please help