Comment System with jQuery, Ajax and PHP (Version 2.0).

Three months back I had posted an popular article Comment system with jQuery, Ajax and PHP.. Most of the readers commented about displaying existing(old) comments and database design. So in this post I had updated the old code.

219
comments:

Sri, the script is great . I edited your old one and made it just like this .

The only problem is > suppose there is a post that having 30 or more comments then you do not want to show all of them on a single page .. its a great thing if we add your "pagination" script with this script so it will become very handy but in this case we have to append latest comment on the top ? will it work ?

Excuse me people but who the hell do you think you are commenting bad and rude replies and also demanding things. These people are sharing their work for free and have given their time. I suggest you all go and write it from scratch if you dont like you lazy son of a b***hes! And sql injection ...if your 'clever' enough to pick up on the fact that a script requires sanitising then you SHOULD be clever enough to actually sanitise the input data yourself. Blimey .... next you'll be asking for this guy to code your fricken websites!

Be fair and respect the fact that you are getting something FOR FREE! Dont moan and be POLITE! Or like I said ... stop be so god damn lazy and write the code yourself!!!!!!!!!!!!!

then jQuery will automaticly encode it for you, and that will fix the '&' break problem.

2. another bug is if you force new lines in the textarea the output will still be on one line.to fix this you must use the php function nl2br() on your $_POST['comment']; before you send it back as a response.

First of all, change "come_email" to "com_email". That's a typo. Second and most importantly, change "$comment_dis" to "$comment".

After I changed that, the comments were inserted into the database. However, everything disappears when the page is refreshed (the comments are still in the database.) I don't know how to fix that problem.

Here are connected with database, ok. But when refresh lost the comment, but if u look in Database the comment are there. I think must be somethink easy to repair. Seems like it can take the data in database to show again.

Hi to all ,i no whats the problome ,the problome is whene we post the comment it save to database only the 2 row and it doesnt save the post_id if you see your databae the 2 row that you posted is there but the post_id id is(0)it shold save the post_id is well .thats why whene you refresh the page it doesnt show the comment . i have the same problome .hope all you guyes andrestand my lital chenglish write

hi there , i finly able to save all coment row to database .but whene i tray the next post on the same article id it get agin 0 post_id so it make me lital confuse cus i can`t post more then 1 post in article is any one there have the same problome i have

Very simple but very effective. I had it working in minutes, but for one bug I noticed: you have two inputs with the id of "name" which causes the form to be invalid and not be sent. Once that was fixed, it just worked.

I had error :Notice: Undefined variable: post_id in C:\wamp\www\comment_updata\comment.php on line 158which is :$sql=mysql_query("select * from comments where post_id_fk='$post_id'");what do? for that , sorry I just start learn PHP and java ?can anyone help me plz ? thank you

Hi Srinivas and all the good people over there!The script is great. I'm adapting it to use it with our Portal. The problem comes when I use special chars , like á é í ó ú or ñIt seems that the jquery stuff converts them into very strange chars and I cannot use them. I tested taking out all jquery stuff, and doing an old and common form post to commentajax.php and it works pretty fine. Perhaps I'm missing something? or there is an extra configuration that can be added to the jquery stuff? (I'm new to jquery).Another issue that I had and already solved was that, if i refreshed the page, the comments already stored in database were not shown. I just deleted display:none; from comment.php as various people said here.For those that have the problem of comments not being inserted in database at all, I recomend to echo de sql insert statement and run it in phpmyadmin to see the errors it has (it happened to me, I'm using completely different table name and field names, and was having trouble with a not null field I was forgetting to insert)Thanks Srinivas for this great work, and for sharing this with everyone.Sebastián

Hey I love this script, but Im having a problem. Im using 000webhost and made a mysql database and everything. I plugged them into the config.php file but I get this message "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a2061535/public_html/comment.php on line 159" So I would love any help :D

success: function(html){$("ol#update").append(html);$("ol#update li:last").fadeIn("slow");$("#flash").hide();}fadeIn didn't work for me, I had to .hide() it first before fadeIn(), otherwise it wouldn't work.

for the users which still have problem with '&' and ( next line ) :for solving '&' problem, in JavaScript replace this :var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment+ '&post_id=' + post_id;with this one :var DataString = {'name': name,'email': email,'comment': comment,'post_id': post_id }and for solving next line problem, in commentajax.php after this line :$comment=$_POST['comment'];add this :$comment = nl2br($comment_dis);$comment_insert = mysql_real_escape_string($comment_dis);and when you want to insert the comment to database, insert $comment_insert instead of $comment_dis.

in the above comment, for solving '&' problem, you must use dataString, not DataString ( d MUST be lower case, NOT CAPITAL, so use : )var dataString = {'name': name,'email': email,'comment': comment,'post_id': post_id}

Hi. Sorry this might not be a question that i should ask..! bt how can i seperate old comments into pages.. let say show the number of comments on a side and also show only 8 comments in the current page and the rest are given page numbers with about 10 comments per page in date order?

Very good job, man! Whatever those guys, who claim that your code is somehow bad or incorrect, say, don't listen to them, cuz they see nobody else, but themselves. If someone's lookin' for code easy enough to understand it and build complex application based on it, it's not easy to find such code, because internet is full of arrogant asshol*s, who make it incredibly complicated only to point at their own geniousness! Thx again, mister!

Hi,I like this script, good job.But i do not understand what we will post and from where we will get data to post in posts table.We have FK in comment table but we cant get any data from post_id if we don't write something in.

hi thanks for ur job .. i made it but a message appear to me when i try it Notice: Undefined variable: post_id in C:\wamp\www\site\comment.php on line 184 Call Stack #TimeMemoryFunctionLocation 10.0018373568{main}( )..\comment.php:0 "/>can help me to fix it

Hi there,trying to include this one into my page, but it seems like it's firing to another form which is present at my page. Checking for the comment field and saying it's not filled out. So as I'm really new to this ajax stuff, how can I get the script in checking forms[1] instead of the first one?If I'm trying it's not giving any error message but also not adding the comment to DB.

Hey I used your script however I noticed that sometimes the comments won't add. It will not display on the page or be added to the database. It is not a problem with the query because it adds other records. It's as if it works only sometimes.

Srinivas, thanks for sharing. I believe this page is using the Blogger/Google comments system.. . I'm wondering myself whether to use the comments with Blogger, or build my own (probably taking advantage of your tutorial).. With the one you built, would there be problems with spam I wonder...?

Great script - thank you so much to share it with us! By the way the foreign key would work only when using innoDB as mysql engine. I'm generally using MYIsam so I've got to change the code. Not a problem for me but may be worth mentioning.

... but it didn't work. You've got some realy big bugs inside that prevent the code from work.

1)There is no element with ID="post". Instead there're two ID="name". I guess you overseen that simply. As consequence the post_id cant be submitted and without the value for the foreign key the query won't work.

2) Anyways the query is buggy. It's like this:mysql_query("insert into comment(com_name,come_email,com_dis) values ('$name','$email','$comment_dis','$post_id')");

I'm trying to come up with a way to have several different comment boxes on the same page. The blog will have numerous short articles and needs a separate comment section for each article. Is this possible with your system? If so, please provide some direction. Thank you.

PHP code can be simply mixed with HTML code, or it can be used in combination with various templating engines and web frameworks. PHP code is usually processed by a PHP interpreter, which is usually implemented as a web server's native module or a Common Gateway Interface (CGI) executable.