2011-04-26 3 views
0

jQuery를 사용하여 페이지를 다시로드하거나 주석을 삭제하지 않고도 주석을 적용하는 웹 사이트의 주석 섹션이 있습니다.PHP에서 데이터를 다시 자바 스크립트 파일로 반환 하시겠습니까?

지금 당장은 주석을 작성하면 외부 JS로 보낸 다음 PHP가 처리하는 PHP 파일로 보냅니다. 성공한 경우 댓글 섹션에 댓글을 추가합니다. 내 삭제 작업 : jQuery는 mysql 데이터베이스에서 내 코멘트 ID를 삭제합니다.

그래서 내 문제는 jQuery를 통해 삭제 버튼을 추가하고 어떻게 든 자바 스크립트 파일을 다시 호출하여 삭제 버튼이 삭제할 파일을 알 수 있도록 삭제 버튼이 양식에 넣을 ID를 알고 있도록 알려줍니다. ? 사전에

$(function() { 

$(".commentbutton").click(function() { 

$.ajax({ 
    type: "POST", 
    url: "http://myflashpics.com/process_addcomment.php", 
    data: $("#commentform").serialize(), 
    success: function() { 

    var theComment = $("#commentsss").val(); 
    var theUserId = $("#user_id_two").val(); 
    var thePhotoId = $("#photo_id").val(); 
    var theProfilePicture = $("#user_profile_picture").val(); 
    var theUsername = $("#user_username").val(); 

    // Get new HTML data 
    var html = "<div class='comment_odd'><img src='" + theProfilePicture + "' class='comment_thumbnail'/><div class='comment_username'>" + theUsername + "</div><div class='comment_text'>" + theComment + "</div></div>"; 

    // Append, then fade in 
    $(html).hide().appendTo(thecommentsdisplay).fadeIn(500); 

    } 
}); 
return false; 
}); 
}); 

감사 :

여기 내 add_comment 스크립트입니다!
Coulton

편집 1 : 여기

내 의견 양식이다 (단지 명확하게) :

여기
user_id_two (the user's ID) 
commentsss (comments field) 
photo_id (the id of the photo being commented on) 
user_profile_picture (profile to display on the user's profile picture in the banner) 
user_username (username of the user commenting) 

의도 내 삭제 버튼 형태 : 마지막으로

<form method='post' action='' name='deleteform' id='deleteform'> 
<input type='hidden' name='userid' value='<?php echo "$userid_session"; ?>' /> 
<input type='hidden' name='userpass' value='<?php echo "$password_session"; ?>' /> 
<input type='hidden' name='pictureid' id='pictureid' value='<?php echo "$picture_id"; ?>' /> 
<input type='hidden' name='profilepictureid' id='profilepictureid' value='<?php echo "$user_profile_picture_id"; ?>' /> 
</form> 

그리고 내 DELETE COMMENT jQuery :

$(function() { 

$(".commentdeletebutton").click(function() { 

var className = $(this).attr('class'); 
var theID = className.replace(/delete_button commentdeletebutton delete_(\d+)/, "$1"); 
commentDone = "#comment_" + theID; 
formDone = "#commentdeleteform_" + theID; 

$.ajax({ 
    type: "POST", 
    url: "http://myflashpics.com/process_deletecomment.php", 
    data: $(formDone).serialize(), 
    success: function() { 

    $(commentDone).hide(); 

    } 
}); 
return false; 
}); 
}); 
+0

나는 혼란 스러워요. 우선, 몇 가지 ID가 있지만 우리는 설정/사용하지 않는 것을 보여줍니다. 두 번째로, 귀하의 코드는 모두 타의 추종을 불문하고 혼란 스럽습니다. – Christian

+0

내 업데이트를 확인하십시오. – iosfreak

+0

전체 양식을 삭제해야하는 이유가 확실하지 않은 이유는 무엇입니까? – morgar

답변

1

삭제할 링크를 추가하면 삭제를위한 전체 URL (예 : http://myflashpics.com/process_removecomment.php?id=xx)을 href에 삽입 할 수 있습니다. 따라서 클릭 이벤트를 해당 링크에 바인딩하고 $(this).attr('href')을 사용하여 올바른 URL을 얻고 ajax를 사용하여 삭제를 수행 할 수 있습니다. 보다 완전한 샘플

편집 됨 :

<? [[Loop your recordset]] { ?> 
<div class="comment"> 
    <a href="http://myflashpics.com/process_deletecomment.php?id=<?=$id?>">Delete</a> 
    <div class="comment"> 
     [[Comment content...]] 
    </div> 
</div> 
<? } ?> 

<script> 
$(function() { 
    $(".commentdeletebutton").click(function() { 
     $this = $(this); 
     $.ajax({ 
      url: $this.attr('href'), 
      success: function(data) { 
       $this.parent().slideUp('fast', function() { 
        $this.parent().remove(); 
       }); 
      } 
     }); 
     return false; 
    }); 
}); 
</script> 
+0

제 편집을 확인하십시오. – iosfreak

+0

나는 당신의 소스에서 user/pw를 볼 수있는 아주 나쁜 생각이라고 생각한다. 그런 식으로해서는 안된다. 로그인시에만 user/pw를 사용해야하며, 그 후에는 세션을 활성 상태로 유지하면됩니다. – morgar

+0

이것은 문제가되지 않습니다. 그것은 잘 작동합니다. ID는 데이터베이스의 AUTO INCREMENT 필드에서옵니다. ID를 얻는 유일한 방법은 그것을 처리하는 PHP 파일에서 물건을 다시 얻는 것입니다 ... 제 문제를 보시겠습니까? – iosfreak

관련 문제