i'm using below php pdo , ajax program submit user comment on post. @ first when did this, work fine stopped working doing wrong can fix me or decide better way achieve it?
problem currently, when submit reply post database not return message server. example: when post "hello"
expect instantly show without reloading page.
here ajax
<script> $(document).ready(function(){ $('#add-comment').submit(function() { var comment = $('#comment').val(); var name = $('#anony').val(); var rid = $('#rid').val(); $('#response-out').html("<i class='fa fa-spinner fa-spin' style='font-size:19px;'></i>"); $.ajax({ type: 'post', url: '/alter_reply.php', data: 'comment='+comment+'&name='+name+'&rid='+rid, }) .done(function(data){ $('#response-out').html(data); }) .fail(function() { alert( "posting failed." ); }); return false; }); }); </script>
alter_reply.php
<?php if($_post){ $db_host = "localhost"; $db_user = "root"; $db_pass = "pass"; $db_name = "cods"; try { $db_conn = new pdo("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass); $db_conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); $stmt = $db_conn->prepare("insert replys(rid,mesreplys,rtime,rusername) values(:rid,:mesreplys,:rtime,:rusername)"); $stmt->bindparam(':rid', $rid); $stmt->bindparam(':mesreplys', $comment); $stmt->bindparam(':rtime', $timedate); $stmt->bindparam(':rusername', $user); $form = $_post; $rid = mysql_real_escape_string($form['rid']); $comment = mysql_real_escape_string($form['comment']); $timedate = date("y-m-d h:i:s"); if(isset($_session['username'])){ $user = $_session['username']; }else{ $anony_user = mysql_real_escape_string($form['name']); $user = $anony_user; } $stmt->execute(); //i don't know how again why echo submitted values //if can more better me appricite echo "<td><table> <tbody> <tr> <td class='comment-score'> </td> <td> </td> </tr> </tbody> </table> </td>"; echo "<td class='comment-text'> <div style='display: block;' class='comment-body'>"; echo "<span class='comment-copy'>"; echo "$comment"; echo "</span> <a href='' class='comment-user'>"; echo "$user"; echo "</a> <span class='comment-date' dir='ltr'> @<a class='comment-link' href='#'><span title='' class='relativetime-clean'>"; echo "$timedate"; echo "</span></a></span></div></td>"; } catch(pdoexception $e) { echo "error:" . $e->getmessage(); } $db_conn = null; } ?>
here html
<div id="response-out" class='comment'> </div> <form id="add-comment" action="javascript:void(0);" style="font-size: 100%;"> <textarea placeholder="" name="comment" cols="68" rows="3" style="min-height:30px;" id="comment" required="true"></textarea> <br/> <?php if(!isset($_session['username'])) { echo "<label>enter name</label><br>"; echo "<input placeholder='enter name' style='width:130px;height: inherit;' required='true' id='anony' type='text' name='name'/>"; }?> <input tabindex="0" value="add comment" class="btnpostq" id="donedbtn" type="submit"/> <input type="hidden" value="<?php echo $_get['postid'];?>" name="rid" id="rid"/> </form>
first : please check php error displayed. then, suppose using debugger firebug check javascript , ajax request. if not, configure php error , install debugger browser. if yes, please confirm have no js error nor php error returne ajax.
the purpose of answer first clean code , try avoid errors.
javascript :
<script> $(document).ready(function(){ $('#add-comment').submit(function() { var comment = $('#comment').val(); var rid = $('#rid').val(); // check if anony exist avoid js error if(0 < $('#anony').length) { var name = $('#anony').val(); var datatosend = 'comment='+comment+'&rid='+rid; } else { var name = ''; var datatosend = 'comment='+comment+'&rid='+rid; } var datatosend = $('#response-out').html("<i class='fa fa-spinner fa-spin' style='font-size:19px;'></i>"); $.ajax({ type: 'post', url: '/alter_reply.php', data: datatosend, }) .done(function(data){ $('#response-out').html(data); }) .fail(function() { alert( "posting failed." ); }); return false; }); }); </script>
php :
if(0 < sizeof($_post)) { //first retrieve data $form = $_post; $rid = $form['rid']; $comment = $form['comment']; $timedate = date("y-m-d h:i:s"); if(isset($_session['username'])){ $user = $_session['username']; }else{ $anony_user = $form['name']; $user = $anony_user; } $db_host = "localhost"; $db_user = "root"; $db_pass = "pass"; $db_name = "cods"; try { $db_conn = new pdo("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass); $db_conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); $stmt = $db_conn->prepare("insert replys(rid,mesreplys,rtime,rusername) values(:rid,:mesreplys,:rtime,:rusername)"); //now have first retrieve data, can bind $stmt->bindparam(':rid', $rid); $stmt->bindparam(':mesreplys', $comment); $stmt->bindparam(':rtime', $timedate); $stmt->bindparam(':rusername', $user); $stmt->execute(); } catch(pdoexception $e) { echo "error:" . $e->getmessage(); } $db_conn = null; //i don't know how again why echo submitted values //if can more better me appricite //cleaning html : tags crossing , including spans in spans, not sure usefull echo '<table>'. '<tbody>'. '<tr>'. '<td class="comment-score">'. ' '. '</td>'. '<td>'. ' '. '</td>'. '</tr>'. '<tr>'. '<td class="comment-text">'. '<div style="display: block;" class="comment-body">'. '<span class="comment-copy">'.$comment.'</span>'. '<a href="" class="comment-user">'.$user.'</a>'. '<span class="comment-date" dir="ltr">'. '@<a class="comment-link relativetime-clean" href="#">'.$timedate.'</a>'. '</span>'. '</div>'. '</td>'. '</tr>'. '</tbody>'. '</table>'; } else { echo 'no data'; }
Comments
Post a Comment