簡體   English   中英

將數據從PHP返回到javascript文件?

[英]Return data from PHP back to a javascript file?

我的網站上有一個評論部分,該部分使用jQuery為評論添加動畫,而無需重新加載頁面以及刪除評論。

現在,我有了它,因此當您編寫注釋時,它會將其發送到外部JS,然后發送到處理它的php文件。 如果成功,它將注釋添加到注釋部分。 我的刪除操作:jQuery刪除我在mysql數據庫中的注釋ID。

所以我的問題是我想通過jQuery添加一個刪除按鈕,然后以某種方式調用javascript文件並告訴它ID,以便刪除按鈕知道要放入表單的ID,以便刪除文件知道要刪除什么?

這是我的add_comment腳本:

$(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;
});
});

提前致謝!
庫爾頓

編輯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;
});
});

如果添加要刪除的鏈接,則可以在href中插入要刪除的完整URL,即http://myflashpics.com/process_removecomment.php?id=xx 因此,您可以將click事件綁定到該鏈接,並使用$(this).attr('href')獲得正確的網址,並使用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>

暫無
暫無

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

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