php - comment-reply jquery gets reply and store it in table but not comment_id -
i have created comment-reply system usign php , jquery. code includes function comments , using jquery trying comments_id id each comment , reply , save table called comments_reply. problem cannot comments_id, can successfuly reply , store in comments_reply table. idea why code cannot comments_id store it?
this function in php including jquery:
<?php <script> $(document).ready(function(){ $('.reply').keyup(function(e){ if(e.keycode == 13){ var comments_id = $(this).attr('comments_id'); var reply = $(this).val(); $.post('reply.php', {comments_id:comments_id, reply:reply}); $('.reply').val(''); } }); }); </script> function getcomments(){ $comments = ""; $sql = mysql_query("select * comments order comment_date desc ") or die (mysql_error()); if(mysql_num_rows($sql) == 0){ $comments = " <div class='each_comment'> there no comments ...</div> "; } else { while ($row= mysql_fetch_assoc($sql)){ $comments .= "user says : <div class='each_comment'> ".$row['comment_date']."".$row['comment']." <input type='text' class='reply' comments_id='<?php ".$row['comments_id']." ?>' /> </div> "; } } return $comments; } ?>
and page: reply.php
<?php $comments_id = $_post['comments_id']; $reply = $_post['reply']; mysql_query("insert comments_reply values ('', '$comments_id', '$reply') "); ?>
to prevent confusion between value , propery name (being same) when submitting server should add quotes name.
$('.reply').keyup(function(e){ if(e.keycode == 13){ var comments_id = $(this).attr('comments_id'); var reply = $(this).val(); $.post('reply.php', {"comments_id":comments_id, "reply":reply}); $('.reply').val(''); } });
that may fix issue
edit
i change custom html attribute names so:
change comments_id
data-comment-id
, reply
data-reply
sake of convention , possibly usefull html5 support , don't forget update usages (in html , javscript code).
i have noticed sql code values ('', '$comments_id', '$reply')
shows inserting blank value first column in table there reason this?
edit2
for when people @ answer instead of reading comments of answer put here:
replace
<input type='text' class='reply' comments_id='<?php ".$row['comments_id']." ?>' />
with
<input type='text' class='reply' comments_id='{$row['comments_id']}' />
or id include unwanted characters.
Comments
Post a Comment