1
我想通過ajax和php做一個評論系統。此代碼只提交第一篇文章評論。其他人發表評論無效。當其他評論發佈並進入其重新加載頁面時。只有第一篇文章評論完美。請幫我解決這個問題。ajax多個表單提交輸入按
我的HTML:
$data = $con->query($sql);
if($data->num_rows>0){
while($row = $data->fetch_array(MYSQLI_ASSOC))
{
echo '<div class="box-footer">';
echo '<form id="comment_form" name="comment_form" method="post">';
echo '<img class="img-responsive img-circle img-sm" src="http://localhost/admin/dist/img/user1-128x128.jpg" alt="Alt Text">';
echo '<div class="img-push">';
echo '<input type="hidden" name="post_id" id="post_id" value="'.$id.'">';
echo '<input type="text" class="form-control input-sm" name="comment" id="comment" placeholder="Press enter to post comment">';
echo '</div>';
echo '</form>';
echo '</div>';
}
}
提交功能是:
$(document).ready(function(){
$("#comment_form").submit(function(e){
e.preventDefault();
if (document.getElementById("comment").value == "") {
swal("ERROR", "Please write a comment first", "error");
} else {
var user_id = <?php echo $user_id; ?>;
var post_id = document.getElementById("post_id").value;
var comment = document.getElementById("comment").value;
var dataString = 'userid=' + user_id + '&post_id=' + post_id + '&comment=' + comment;
$.ajax({
type: "POST",
url: "commentupload.php",
data: dataString,
cache: false,
success: function(html) {
var status = html;
if(status == 0){
swal("Success", "Comment Added!", "success");
post_id = "";
comment = "";
document.getElementById("post_id").value = "";
document.getElementById("comment").value = "";
}
else if(status == 1) {
swal("ERROR", "Something went wrong!", "error"); location.reload();
}
else {
swal("ERROR", status, "error");
}
}
});
return false;
}
});
});
如果我是你,我會創建多個領域,而不是創建多個形式,因此,您可以在爆炸到一個數組 –