2011-04-26 10 views
0

jQueryを使用してページをリロードせずにコメントをアニメーション化したり、コメントを削除したりする私のウェブサイトにはコメント欄があります。PHPからのデータをjavascriptファイルに戻しますか?

今はコメントを書くときに外部のJSに送信し、次にPHPのファイルに送信して処理します。成功した場合は、コメントセクションにコメントを追加します。私の削除アクション:jQueryはmysqlデータベースのコメントIDを削除します。

私の問題は、jQuery経由で削除ボタンを追加し、何とかjavascriptファイルにコールバックしてidに伝えるので、削除ボタンはフォームに入れる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

EDIT 1:

は、ここに私のコメントフォーム(単に明確にする)です:

<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:

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) 

はここにも私の削除ボタン形ですコメント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

削除するリンクを追加すると、hrefに完全なURL(例:http://myflashpics.com/process_removecomment.php?id=xx)を挿入できます。 したがって、クリックイベントをそのリンクにバインドし、$(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

関連する問題