0

I'm trying to insert my comment content with Ajax. But I believe I'm having trouble with the comment_add.php page and wondered if someone could take a look for me.

Getting the streamid seems to work as I've checked in firebug, but it shows no content. So I don't know whether I may have missed something that I can't see but someone else might be able to find. Or maybe I just haven't written the comment_add page properly.

FORM

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

AJAX

<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']);
}
}
?>

FUNCTION

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

2 Answers 2

1

change

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

to

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

because content doesn't exist in that function

AND

your links wouldn't be <a>'s

EDIT 2 Because you wanted to add the data from the other side here is a little hack you can use to get content

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

This is possible because the constructor loop through the object and sets it to this

Sign up to request clarification or add additional context in comments.

3 Comments

Actually response would only be used if you're calling data. I can use VAR content as its already been added as content into the database. Your solution hasn't solved my problem.
Fantastic, that did the trick. Thankyou @EaterOfCorpses. It works now. What is the constructor loop? Why did I have to do that?
No i was trying to explain that the constructor is just function(options){for(i in options){this[i] = options[i];}}
0

Your input field "content" markup is missing value and type attribute. Type is not crucial as it defaults to text but always better to explicitly specify.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.