【发布时间】:2016-01-29 09:32:01
【问题描述】:
我正在使用下面的 php PDO 和 ajax 程序来提交用户对帖子的评论。 起初我这样做时,它工作正常,但现在它停止工作我做错了什么,有人可以为我解决这个问题或决定一个更好的方法来实现它吗?
问题
目前,当我提交回复时,它会发布到我的数据库,但不会从服务器返回消息。
示例:当我发布"hello" 时,我希望它会立即显示它而无需重新加载页面。
这里是 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='comcopy'>";
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;
}
?>
这里是 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>
【问题讨论】:
-
请参阅:
$rid = mysql_real_escape_string($form['rid']);、$comment = mysql_real_escape_string($form['comment']);和$anony_user = mysql_real_escape_string($form['name']);,您正在混合 API。 -
@RajdeepPaul 当我删除
mysql_real_escape_string时也是同样的事情@ -
您没有在 alter_reply.php 页面的任何地方开始会话。在 php 脚本的最顶部添加这一行
<?php session_start(); ?>。 -
我确实开始了会话,它发布得很好,但我希望它在不刷新页面的情况下回显数据