Php 使用Ajax插入注释内容

Php 使用Ajax插入注释内容,php,javascript,jquery,html,ajax,Php,Javascript,Jquery,Html,Ajax,我试图用Ajax插入我的评论内容。但是我相信我在comment_add.php页面上遇到了问题,不知道是否有人可以帮我看看 获取streamid似乎可以工作,因为我已经签入了firebug,但是它没有显示任何内容。所以我不知道我是否错过了一些我看不见但其他人可能能找到的东西。或者我只是没有正确地写评论添加页面 形式 echo” "; 阿贾克斯 $(文档).ready(函数(){ $(“form#addcomment”).submit(函数(事件){ event.preventDefault(

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

获取streamid似乎可以工作,因为我已经签入了firebug,但是它没有显示任何内容。所以我不知道我是否错过了一些我看不见但其他人可能能找到的东西。或者我只是没有正确地写评论添加页面

形式

echo”
";
阿贾克斯


$(文档).ready(函数(){
$(“form#addcomment”).submit(函数(事件){
event.preventDefault();
var content=$(“#content”).val();
var streamid=$(“#streamid”).val();
$.ajax({
类型:“POST”,
url:“comment_add.php”,
数据类型:“json”,
数据:{content:content,streamid:streamid},
成功:功能(响应){
$(“#commentaddid”).prepend(“+content+”);
}
});
});
});
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;
    }
公共函数添加注释($streamid,$content){
$content=mysql\u real\u escape\u字符串($content);
$content=带标签($content);
$content=preg_replace('/(?change

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

因为该函数中不存在内容

你的链接不会是

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

$.ajax({
type: "POST",
url: "comment_add.php", 
dataType: "json",
_content:content,
data: {content:content,streamid:streamid},  
success: function(response){  
    $("#commentaddid").prepend(""+this._content+""); 
} 
});
这是可能的,因为构造函数循环对象并将其设置为该值。您的输入字段“内容”标记缺少类型属性。类型并不重要,因为它默认为文本,但最好显式指定

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


实际上,只有在您调用数据时才会使用响应。我可以使用VAR content,因为它已经作为内容添加到数据库中。您的解决方案没有解决我的问题。太棒了,这就成功了。谢谢@eaterofcasters。它现在起作用了。构造函数循环是什么?我为什么要这样做?不,我试图解释一下构造函数只是
函数(options){for(i in options){this[i]=options[i];}}}
success: function(response){ 
$("#commentaddid").prepend(""+response+"");
}
$.ajax({
type: "POST",
url: "comment_add.php", 
dataType: "json",
_content:content,
data: {content:content,streamid:streamid},  
success: function(response){  
    $("#commentaddid").prepend(""+this._content+""); 
} 
});
<input name="content" type="text" value="" id="content" placeholder="Comment.." autocomplete="off" />