Bantuan untuk membuat sistem komentar menggunakan PHP, jQuery dan AJAX

Saya sedang berupaya membuat sistem komentar untuk plugin acara yang sedang saya kerjakan. Saya memiliki komentar utama yang berfungsi dengan baik dan disimpan ke database dan ditampilkan di halaman. Yang saya perlukan bantuannya sekarang adalah menampilkan formulir balasan ketika Balasan diklik di bawah setiap pesan, kemudian menampilkan balasan dengan komentar utama. Salah satu masalah yang saya alami adalah saya menggunakan MySQL versi lama jadi saya tidak bisa menggunakan kueri rekursif. Bantuan apa pun dihargai. Kode yang saya miliki ada di bawah.

Membentuk

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="commentform" class="commentform">
    <textarea name="comment" id="comment" class="commentbox" rows="5" cols="120" required></textarea><br />
    <input type="hidden" name="eventid" id="eventid" value="<?php echo $id; ?>" />
    <input type="hidden" name="userid" id="userid" value="<?php echo $userid; ?>" />
    <input type="hidden" name="action" value="commentform">
    <button class="commentbtn">Add Comment</button>
</form>

jQuery/AJAX

jQuery(function ($) {
    $('.commentform').submit(function () {
        event.preventDefault();
        var commentform = $('.commentform');
        $.ajax({
            url: commentform.attr('action'),
            data: commentform.serialize(), // form data
            type: commentform.attr('method'), // POST
            beforeSend: function (xhr) {
                commentform.find('button').text('Processing...');
            },
            success: function (data) {
                commentform.find('button').text('Add Comment');
                $(".commentform")[0].reset();
                $('#response').html(data); // insert data
            }
        });
        return false;
    }); 
});

Fungsi PHP

add_action('wp_ajax_commentform', 'comment_submit');
add_action('wp_ajax_nopriv_commentform', 'comment_submit');
function comment_submit() {
    global $wpdb;
    date_default_timezone_set("America/New_York");
    $date = date('Y-m-d H:i:s');
    $inserttable = $wpdb->prefix . "event_comments";
    $eventid = $_POST["eventid"];
    $userid = $_POST["userid"];
    $comment = $_POST["comment"];

    $data = array(
        'eventid' => $eventid,
        'userid' => $userid,
        'content' => $comment,
        'datesaved' => $date,
    );

    $wpdb->insert($inserttable, $data);
}

Tampilkan Komentar

$commentctr = $wpdb->get_var("SELECT count(*) FROM " . $wpdb->prefix . "cmc_communitybb_event_comments where eventid=$eventid");

echo "<h2>Comments</h2>";
echo "<h3>$commentctr Total Comments</h3>";

$commentsql = $wpdb->get_results("SELECT comm.id as id, comm.eventid as eventid, comm.userid as userid, comm.content as comment, comm.datesaved as date, users.meta_value as fname, users2.meta_value as lname
                                FROM " . $wpdb->prefix . "event_comments as comm
                                inner join " . $wpdb->prefix . "usermeta as users on comm.userid = users.user_id and users.meta_key = 'first_name'
                                inner join " . $wpdb->prefix . "usermeta as users2 on comm.userid = users2.user_id and users2.meta_key = 'last_name'
                                where comm.eventid=$eventid and comm.parentid is null order by comm.datesaved asc");

foreach ($commentsql as $comment) {
    $commid = $comment->id;
    $date = date_create($comment->date);
    $fordate = date_format($date, "m/d/Y");
    $fortime = date_format($date, "g:i a");
    ?>
    <div id="commentdiv<?php echo $commid; ?>" class="commentdiv">
        <b>Comment by: </b><?php echo $comment->fname . " " . $comment->lname; ?><br />
        <b>Date/Time: </b><?php echo $fordate . " - " . $fortime; ?><br />
        <?php  echo $comment->comment . "<br>"; ?>
        <a href="javascript:void(0);" id="replylink<?php echo $commid; ?>" class="replylink" onclick="showreplybox(<?php echo $commid; ?>)">REPLY</a>
    </div>
    <hr class="commenthr" />
    <?php
}

Leave a Reply

Your email address will not be published. Required fields are marked *