使用 Ajax 插入注释内容


Insert comment content with Ajax

我正在尝试用Ajax插入我的评论内容。但我相信我在comment_add.php页面上遇到了问题,想知道是否有人可以帮我看看。

获取流 id 似乎在我签入 firebug 时有效,但它没有显示任何内容。所以我不知道我是否错过了一些我看不到的东西,但其他人可能会找到。或者也许我只是没有正确编写comment_add页面。

形式

echo "<form id='addcomment' method='POST' class='form_statusinput'>
<input type='hidden' name='posterid' id='posterid' value='".$user1_id."'>
<input type='hidden' name='streamid' id='streamid' value='".$streamitem_data['streamitem_id']."'>
<input name='content' id='content' placeholder='Comment..' autocomplete='off'>
<input type='submit' id='button' value='Feed'>
</form>";

阿贾克斯

<script>
$(document).ready(function(){
$("form#addcomment").submit(function(event) {
event.preventDefault();
var content = $("#content").val();
var streamid = $("#streamid").val();
$.ajax({
type: "POST",
url: "comment_add.php",
dataType: "json",
data: {content:content,streamid:streamid}, 
success: function(response){ 
$("#commentaddid").prepend(""+content+"");
}
});
});
});
</script>

COMMENT_ADD。.PHP

<?php
session_start();
require"include/load.php";
$user1_id=$_SESSION['id'];
if(isset($_POST['streamid'])&isset($_POST['content'])){
if($_POST['content']){
rawfeeds_user_core::add_comment($_POST['streamid'],$_POST['content']);
}
}
?>

功能

public function add_comment($streamid,$content){
            $content =  mysql_real_escape_string($content);
            $content =  strip_tags($content);
            $content = preg_replace('/(?<!S)((http(s?):'/'/)|(www.))+(['w.1-9'&=#?'-~%;'/]+)/','<a href="http$3://$4$5">http$3://$4$5</a>', $content);
            if(strlen($content)>0){
            $insert = "INSERT INTO streamdata_comments(comment_poster, comment_streamitem, comment_datetime, comment_content) VALUES (".$_SESSION['id'].",$streamid,UTC_TIMESTAMP(),'$content')";
                        echo $insert;
            $add_post = mysql_query($insert) or die(mysql_error());
            }
            return;
    }

更改

success: function(response){ 
$("#commentaddid").prepend(""+content+"");
}

success: function(response){ 
$("#commentaddid").prepend(""+response+"");
}

因为该函数中不存在内容

您的链接不会<a>

编辑 2因为您想从另一边添加数据,所以这里有一个小技巧,可以用来获取内容

$.ajax({
type: "POST",
url: "comment_add.php", 
dataType: "json",
_content:content,
data: {content:content,streamid:streamid},  
success: function(response){  
    $("#commentaddid").prepend(""+this._content+""); 
} 
});

这是可能的,因为构造函数循环遍历对象并将其设置为

您的输入字段"content"标记缺少类型属性。类型并不重要,因为它默认为文本,但最好明确指定。

<input name="content" type="text" value="" id="content" placeholder="Comment.." autocomplete="off" />