簡體   English   中英

如何在評論之間附加“評論回復”

[英]How to append “comment reply” between comments

我正在為我的網站開發一個評論框。

我正在UL(列表)中顯示消息。 每個屬於LI的UL子級都包含一個顯示消息的表

顯示我的 評論的圖片的 網址 評論圖像

我的消息正在使用ajax和php顯示

ajax腳本將數據發送到php腳本,然后將其變異到數據庫中,同時它也在屏幕上更新

問題當我刷新頁面或加載整個頁面時,消息以正確的順序顯示

但是,當我想回復以前添加的消息(使用Ajax)時,所有評論的末尾都會顯示“回復”,而不是應顯示的位置。

例如

在圖像中,如果我回復此消息,則注釋為“ DAVID”,它顯示在消息名稱“ Abdul Rehman Javed Khan 0123456789”之后,這是最后一條消息。

HTML

             <ol  class="timeline" id="update">                                  
        <?
        $retrieve = $con->select1("*", "`comments`", "page_id=" . $page_id);
        $confirm_author = 0;            
        while ($row = mysql_fetch_array($retrieve)) {
            echo'<table class="shw-comment" id="show-comment-table" ><tbody><tr>';
            $name = $con->verify_author($row['name'], $row['email'], $row['website']);                
            echo'<td align="left" colspan="9" id="name-td">' . $name;
            $niceDay = $con->perfect_date_format($row['date_and_time']);
            echo'<div align="right" id="comment-date">' . $niceDay . '</div></td></tr><tr>';
            echo'<td align="left" id="user-icon-td">';
            if ($name == "Author") {
                echo '<div class="author" id="user-icon"></div>';
            } else {
                $hash = md5(strtolower(trim($row['email'])));
                $default_usr = urlencode('http://localhost/king-of-developers/images/user-icon.png');
                echo "<div class=\"default-user\" id=\"user-icon\"><img src=\"http://www.gravatar.com/avatar/$hash.'.jpg?s=45&d=$default_usr'\"  /></div>";
            }
            echo '</td>';
            echo'<td colspan="8" id="user-comments-td" valign="top">' . $row['user_comments'] . '</td>';
            echo'</tr><tr>';
            echo'<td align="right" colspan="9" id="reply-td"><input type="button" class="reply" name="reply" value="reply" title="reply"/></td>';
            echo'<tr>
                    <td align="left" colspan="9">
                    <input id="comment-id' . $row['id'] . '" type="hidden" value="' . $row['id'] . '"/>';
            echo'</tr>';
            echo'</tbody></table></li>';
            if($row['respond'] == 1){
                $retrieve2 = $con->select1("*", "`comment_respond`", "comment_id=" . $row['id']);                    
                $confirm_author = 0;
                while ($row2 = mysql_fetch_array($retrieve2)) {
                    echo'<table class="shw-comment" id="comment-reply" ><tbody><tr>';
                    $name = $con->verify_author($row2['name'], $row2['email'], $row2['website']);
                    echo'<td align="left" colspan="9" id="name-td">' . $name;
                    $niceDay = $con->perfect_date_format($row2['date_and_time']);
                    echo'<div align="right" id="comment-date">' . $niceDay . '</div></td></tr><tr>';
                    echo'<td align="left" id="user-icon-td">';
                    if ($name == "Author") {
                        echo '<div class="author" id="user-icon"></div>';
                    } else {
                        $hash = md5(strtolower(trim($row2['email'])));
                        $default_usr = urlencode('http://localhost/king-of-developers/images/user-icon.png');
                        echo "<div class=\"default-user\" id=\"user-icon\"><img src=\"http://www.gravatar.com/avatar/$hash.'.jpg?s=45&d=$default_usr'\"  /></div>";        
                    }
                    echo '</td>';
                    echo'<td colspan="8" id="user-comments-td" valign="top">' . $row2['user_comments'] . '</td>';
                    echo'</tr><tr>';
                    echo'<td align="right" colspan="9" id="reply-td"><input type="button" class="reply" name="reply" value="reply" title="reply"/></td>';
                    echo'<tr>
                            <td align="left" colspan="9">
                            <input id="comment-id' . $row['id'] . '" type="hidden" value="' . $row['id'] . '"/>';
                    echo'</tr>';
                    echo'</tbody></table></li>';
                }    
            }
        }    
        ?>
    </ol>

阿賈克斯

          $('.reply').click(function() {       
    var cId = $(this).closest('table').find('input[id^="comment-id"]');        
    $("#respond").val($(cId).val());
    $("#comments").focus();
});
$(".submit-comment").click(function(){
    var a="",b=0;
    var n=$("#your-name").val();
    var e=$("#your-email").val();
    var w=$("#your-website").val();
    var c=$("#comments").val();
    var pg=$("#page-no").val();
    var rp=$("#respond").val();
    var ch=$("[name=recaptcha_challenge_field]").val();
    var re=$("[name=recaptcha_response_field]").val();
    var confirmAuthor=0;
    n=$.trim(n);
    e=$.trim(e);
    w=$.trim(w);
    c=$.trim(c);
    var h="name="+n+"&email="+e+"&web="+w+"&comment="+c+"&challenge="+ch+"&response="+re+"&respond="+rp+"&page_id="+pg;
    document.getElementById("recaptcha_reload_btn").click();
    if(n==""||e==""||c==""||$.trim(re)==""){
        a+="\n Please Write Your 'Name' , 'Email' , 'Comments' and 'Captcha' Before Submiting. ";
        b++;
    }else{
        var i=/[-_@'$&`~;?%^)*(#!0-9]/;
        var temp=n;
        temp.toLowerCase();
        if(temp=="author"){
            a+="\nInvalid User Name";
            b++;
        }
        if(i.test(n)){
            a+="\nPlease Write a Correct Name ! ";
            b++;
        }
        i=/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
        if(!i.test(e)){
            a+="\nPlease Write Valid Email Address ! ";
            b++;
        }
    }
    if(b>=1){
        alert(a);
    }
    if(b==0){
        $("#flash").show();
        $("#flash").fadeIn(400).html('<img src="images/loading.gif" align="absmiddle"> <span class="loading">Loading Comment...</span>');
        $.ajax({
            type:"POST",
            url:"admin/include/ajax-comments.php",
            data:h,
            cache:false,
            success:function(a){
                $("ol#update").append(a);
                $("ol#update li:last").fadeIn("slow");
                document.getElementById("your-email").value="";
                document.getElementById("your-name").value="";
                document.getElementById("your-website").value="";
                document.getElementById("comments").value="";
                document.getElementById("respond").value="";
                $("#recaptcha_reload_btn").click();
                $("#your-name").focus();
                $("#flash").hide();                    
            }
        })
    }
    return false;
});

PHP

              <?php
        require_once('recaptchalib.php');
        $privatekey = "698754562000000000000000000000233255566";
        $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"],   $_POST["challenge"], $_POST["response"]);
        if (!$resp->is_valid) {
        What happens when the CAPTCHA was entered incorrectly
        die("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
        "(reCAPTCHA said: " . $resp->error . ")");
       } 
      else 
   {
 //Your code here to handle a successful verification
require_once '../config.php';
$con = new config();
if ($_POST) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $web = $_POST['web'];
    $comment = $_POST['comment'];
    $respond = $_POST['respond'];
    $page = $_POST['page_id'];
    $name = $con->clean_input($name);
    $email = $con->clean_input($email);
    $web = $con->clean_input($web);
    $comment = $con->clean_input($comment);
    $name = $con->sanitizeHTML($name);
    $email = $con->sanitizeHTML($email);
    $web = $con->sanitizeHTML($web);
    $comment = $con->sanitizeHTML($comment);
    $con->validateNull($name, "Please Enter Your Name ");
    $con->validateEmail($email, "Please Enter Your Valid Email");
    $con->validateNull($comment, "Please Don't Leave Empty Comments");
    if ($con->errorCounter == 0) {
        $name = ucwords(strtolower($name));
        $comment = ucfirst(strtolower($comment));            
        $con->setTime_zone();
        $comment_time = date('Y-m-d H:i:s', time());
        $comment_time = $con->perfect_date_format($comment_time);
        $ip = $con->getIP();            
        if ($respond >= 1) {
            $col[0] = "comment_id"; 
            $col[1] = "name";
            $col[2] = "email";
            $col[3] = "website";
            $col[4] = "user_comments";
            $col[5] = "user_ip";
            $col[6] = "date_and_time";
            $data[0] = "'" . $respond . "'";
            $data[1] = "'" . $name . "'";
            $data[2] = "'" . $email . "'";
            $data[3] = "'" . $web . "'";
            $data[4] = "'" . $comment . "'";
            $data[5] = "'" . $ip . "'";
            $data[6] = "'" . $comment_time . "'";
            $con->insert("`comment_respond`", $col, $data);
            $con->update("`comments`", "`respond`= 1","`id`='".$respond."'");                
        }
        if ($respond == 0) {
            $col[0] = "page_id";
            $col[1] = "respond";
            $col[2] = "name";
            $col[3] = "email";
            $col[4] = "website";
            $col[5] = "user_comments";
            $col[6] = "user_ip";
            $col[7] = "date_and_time";
            $data[0] = "'" . $page . "'";
            $data[1] = "'" . $respond . "'";
            $data[2] = "'" . $name . "'";
            $data[3] = "'" . $email . "'";
            $data[4] = "'" . $web . "'";
            $data[5] = "'" . $comment . "'";
            $data[6] = "'" . $ip . "'";
            $data[7] = "'" . $comment_time . "'";
            $con->insert("`comments`", $col, $data);                
            $comment_id = mysql_insert_id();
        }
    } else {
        $arraysize = count($con->errorMsg);
        for ($i = 0; $i < $arraysize; $i++) {
            echo $con->errorMsg[$i] . "<br>";
        }
    }
    $confirm_author = 0;
    $con->close_connection();
    }
    }
    ?>
    <li>
            <?php
               if ($respond >= 1){
               echo '<table class="shw-comment" id="comment-reply">';
    }
    if ($respond == 0){
        echo '<table class="shw-comment" id="show-comment-table">';
    }
          ?>    
    <tbody>
        <?
            $name = $con->verify_author($name, $email, $web);
        ?>
        <tr>
            <td align="left" colspan="9" id="name-td">
                    <? echo $name; ?>
                <div align="right" id="comment-date">
               <? echo $comment_time; ?>
                </div>
            </td>
        </tr>
        <tr>                                
            <td align="left" id="user-icon-td">
                <?
                if ($name == "Author") {
                    echo '<div class="author" id="user-icon"></div>';
                } else {
                    $hash = md5(strtolower(trim($email)));
                    $def_usr = urlencode('http://www.kingofdevelopers.com/images/user-icon.jpg');
                    echo "<div class='default-user' id=\"user-icon\"><img  src=\"http://www.gravatar.com/avatar/$hash.'.jpg?s=45&d=$def_usr'\"  /></div>";
                }
                ?>               
            </td>
            <td  colspan="8" id="user-comments-td" valign="top">
              <? echo $comment; ?>
            </td>
        </tr>
        <tr>
            <td align="right" colspan="9" id="reply-td">
                <input type="button" class="reply" name="reply" value="reply" title="reply" />
            </td>
        </tr>
        <tr>
            <td align="left" colspan="9">
                <?
                    if($respond >= 1){
                        $conct ='comment-id'.$respond;
                        echo "<input id='$conct' type='hidden' value=''/>";
                    }
                    if($respond == 0){
                        $conct ='comment-id'.$comment_id;
                        echo "<input id='$conct' type='hidden' value=''/>";
                    }    
                ?>
            </td>
        </tr>
    </tbody>
</table>

Imagelink不再工作,很難理解這個問題。

如果您希望它出現在ol的開頭,請使用:

$("ol#update").prepend(a);

代替:

$("ol#update").append(a);

可能很簡單,但如果沒有,那么我需要更多細節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM