I'm using below php PDO and ajax program to submit user comment on post. At first when I did this, it was work fine but now it stopped working what am I doing wrong can someone fix this for me or decide a better way to achieve it?
Problem
Currently, when I submit reply it will post to my database but it will not return back the message from server.
Example: when I post "hello"
I expect it to instantly show it without reloading page.
Here is 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 INTO 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 to do it again that why i echo back the submitted values
//if you can do it more better for me i will 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 is 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 a 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>