Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否将数据从PHP返回到javascript文件?_Php_Javascript - Fatal编程技术网

是否将数据从PHP返回到javascript文件?

是否将数据从PHP返回到javascript文件?,php,javascript,Php,Javascript,我在我的网站上有一个评论部分,它使用jQuery在不重新加载页面以及删除评论的情况下为评论设置动画 现在,我有它,所以当你写一个评论时,它会将它发送到一个外部JS,然后发送到一个php文件,在那里进行处理。如果成功,它会将注释附加到注释部分。我的删除操作:jQuery删除mysql数据库中的注释ID 所以我的问题是,我想通过jQuery添加一个delete按钮,并以某种方式调用javascript文件,告诉它id,这样delete按钮就知道要在表单中输入的id,这样delete文件就知道要删除什

我在我的网站上有一个评论部分,它使用jQuery在不重新加载页面以及删除评论的情况下为评论设置动画

现在,我有它,所以当你写一个评论时,它会将它发送到一个外部JS,然后发送到一个php文件,在那里进行处理。如果成功,它会将注释附加到注释部分。我的删除操作:jQuery删除mysql数据库中的注释ID

所以我的问题是,我想通过jQuery添加一个delete按钮,并以某种方式调用javascript文件,告诉它id,这样delete按钮就知道要在表单中输入的id,这样delete文件就知道要删除什么

这是我的
添加注释
脚本:

$(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;
});
});
这也是我的删除按钮表单:

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

如果添加删除链接,可以在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>

[[评论内容…]]
$(函数(){
$(“.commentdeletebutton”)。单击(函数(){
$this=$(this);
$.ajax({
url:$this.attr('href'),
成功:功能(数据){
$this.parent().slideUp('fast',function(){
$this.parent().remove();
});
}
});
返回false;
});
});

我很困惑。首先,有几个ID,但您没有向我们显示设置/使用任何ID。第二,你的代码都是不匹配和混乱的。请检查我的更新。不确定为什么你需要一个完整的表单来删除,为什么不需要一个简单的链接?@morgar你不需要一个用于删除的链接;这使得它可以被网络爬虫和链接缓存所接受。因此,itt的最佳做法是只执行类似于从POST请求中删除的操作。@morgar:我想通过jQuery来完成。。。。有没有只包含一个简单链接的代码示例?我认为在源代码中显示user/pw是一个非常糟糕的主意,您不能这样做。您必须仅在登录时使用user/pw,之后只需保持会话的活动状态。这不是问题所在。它很好用。ID来自数据库中的自动增量字段。获取id的唯一方法是从处理它的PHP文件中获取内容。。。看到我的问题了吗?这应该不是问题,我想你应该在
process\u addcomment.php
的数据库中插入新的注释。因此,您可以
回显
id
(或您可能需要返回的任何其他信息),您将在success函数的
data
参数中获得它。一旦你有了它,你可以在删除链接中设置它。询问我是否需要更详细的信息。在类似的情况下,我返回整个html块(包括具有正确ID的delete链接),因为我使用模板系统呈现它。因此,ajaxsuccess函数仅将其附加到页面。
$(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;
});
});
<? [[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>