1// This is a single-line comment
2
3# This is also a single-line comment
4
5/*
6This is a multiple-lines comment block
7that spans over multiple
8lines
9*/
1<?php
2// This is a single-line comment
3
4# This is also a single-line comment
5
6/*
7This is a multiple-lines comment block
8that spans over multiple
9lines
10*/
11
12// You can also use comments to leave out parts of a code line
13$x = 5 /* + 15 */ + 5;
14echo $x;
15?>
1//...
2// If the user clicked submit on comment form...
3if (isset($_POST['comment_posted'])) {
4 global $db;
5 // grab the comment that was submitted through Ajax call
6 $comment_text = $_POST['comment_text'];
7 // insert comment into database
8 $sql = "INSERT INTO comments (post_id, user_id, body, created_at, updated_at) VALUES (1, " . $user_id . ", '$comment_text', now(), null)";
9 $result = mysqli_query($db, $sql);
10 // Query same comment from database to send back to be displayed
11 $inserted_id = $db->insert_id;
12 $res = mysqli_query($db, "SELECT * FROM comments WHERE id=$inserted_id");
13 $inserted_comment = mysqli_fetch_assoc($res);
14 // if insert was successful, get that same comment from the database and return it
15 if ($result) {
16 $comment = "<div class='comment clearfix'>
17 <img src='profile.png' alt='' class='profile_pic'>
18 <div class='comment-details'>
19 <span class='comment-name'>" . getUsernameById($inserted_comment['user_id']) . "</span>
20 <span class='comment-date'>" . date('F j, Y ', strtotime($inserted_comment['created_at'])) . "</span>
21 <p>" . $inserted_comment['body'] . "</p>
22 <a class='reply-btn' href='#' data-id='" . $inserted_comment['id'] . "'>reply</a>
23 </div>
24 <!-- reply form -->
25 <form action='post_details.php' class='reply_form clearfix' id='comment_reply_form_" . $inserted_comment['id'] . "' data-id='" . $inserted_comment['id'] . "'>
26 <textarea class='form-control' name='reply_text' id='reply_text' cols='30' rows='2'></textarea>
27 <button class='btn btn-primary btn-xs pull-right submit-reply'>Submit reply</button>
28 </form>
29 </div>";
30 $comment_info = array(
31 'comment' => $comment,
32 'comments_count' => getCommentsCountByPostId(1)
33 );
34 echo json_encode($comment_info);
35 exit();
36 } else {
37 echo "error";
38 exit();
39 }
40}
41// If the user clicked submit on reply form...
42if (isset($_POST['reply_posted'])) {
43 global $db;
44 // grab the reply that was submitted through Ajax call
45 $reply_text = $_POST['reply_text'];
46 $comment_id = $_POST['comment_id'];
47 // insert reply into database
48 $sql = "INSERT INTO replies (user_id, comment_id, body, created_at, updated_at) VALUES (" . $user_id . ", $comment_id, '$reply_text', now(), null)";
49 $result = mysqli_query($db, $sql);
50 $inserted_id = $db->insert_id;
51 $res = mysqli_query($db, "SELECT * FROM replies WHERE id=$inserted_id");
52 $inserted_reply = mysqli_fetch_assoc($res);
53 // if insert was successful, get that same reply from the database and return it
54 if ($result) {
55 $reply = "<div class='comment reply clearfix'>
56 <img src='profile.png' alt='' class='profile_pic'>
57 <div class='comment-details'>
58 <span class='comment-name'>" . getUsernameById($inserted_reply['user_id']) . "</span>
59 <span class='comment-date'>" . date('F j, Y ', strtotime($inserted_reply['created_at'])) . "</span>
60 <p>" . $inserted_reply['body'] . "</p>
61 <a class='reply-btn' href='#'>reply</a>
62 </div>
63 </div>";
64 echo $reply;
65 exit();
66 } else {
67 echo "error";
68 exit();
69 }
70}
1$(document).ready(function(){
2 // When user clicks on submit comment to add comment under post
3 $(document).on('click', '#submit_comment', function(e) {
4 e.preventDefault();
5 var comment_text = $('#comment_text').val();
6 var url = $('#comment_form').attr('action');
7 // Stop executing if not value is entered
8 if (comment_text === "" ) return;
9 $.ajax({
10 url: url,
11 type: "POST",
12 data: {
13 comment_text: comment_text,
14 comment_posted: 1
15 },
16 success: function(data){
17 var response = JSON.parse(data);
18 if (data === "error") {
19 alert('There was an error adding comment. Please try again');
20 } else {
21 $('#comments-wrapper').prepend(response.comment)
22 $('#comments_count').text(response.comments_count);
23 $('#comment_text').val('');
24 }
25 }
26 });
27 });
28 // When user clicks on submit reply to add reply under comment
29 $(document).on('click', '.reply-btn', function(e){
30 e.preventDefault();
31 // Get the comment id from the reply button's data-id attribute
32 var comment_id = $(this).data('id');
33 // show/hide the appropriate reply form (from the reply-btn (this), go to the parent element (comment-details)
34 // and then its siblings which is a form element with id comment_reply_form_ + comment_id)
35 $(this).parent().siblings('form#comment_reply_form_' + comment_id).toggle(500);
36 $(document).on('click', '.submit-reply', function(e){
37 e.preventDefault();
38 // elements
39 var reply_textarea = $(this).siblings('textarea'); // reply textarea element
40 var reply_text = $(this).siblings('textarea').val();
41 var url = $(this).parent().attr('action');
42 $.ajax({
43 url: url,
44 type: "POST",
45 data: {
46 comment_id: comment_id,
47 reply_text: reply_text,
48 reply_posted: 1
49 },
50 success: function(data){
51 if (data === "error") {
52 alert('There was an error adding reply. Please try again');
53 } else {
54 $('.replies_wrapper_' + comment_id).append(data);
55 reply_textarea.val('');
56 }
57 }
58 });
59 });
60 });
61});