我是AJAX的新手,只是学习绳索。我的问题是我的所有AJAX调用都运行了两次。以下是所有代码和图片:
的Javascript
function loadComments() {
var custid = $('#custid').val();
$.ajax({
type: 'POST',
url: 'php/loadComments.php',
// async: false,
data: {
custid: custid
},
success: function(response){
$('#comments-feed').html(response);
}
});
}
function saveComments() {
var custid = $('#custid').val();
var comment = $('#comment').val();
var username = $('#username').val();
$.ajax({
type: 'POST',
url: 'php/saveComment.php',
data: 'custid='+custid+'&username='+username+'&comment='+comment
}).done(function() {
loadComments();
$('#comment').val('');
});
}
$(document.body).on('click touchstart','#comment-submit',function() {
saveComments();
});
HTML / PHP
<section id="section-underline-5" class="">
<div class="row">
<div class="col-md-12 comments-feed" id="comments-feed">
<?php
if ($comments != 0) {
for ($r=0;$r<$comLength;$r++) {
echo '<div class="panel panel-default comment"><div class="panel-heading">'.$comments[$r][0].'<div class="pull-right"><small>'.$comments[$r][1].'</small></div></div><div class="panel-wrapper collapse in" aria-expanded="true"><div class="panel-body"><p>'.$comments[$r][2].'</p></div></div></div>';
}
}
?>
</div>
</div>
<div class="row comment-input">
<div class="col-md-12">
<form id="comment-form" name="comment-form">
<div class="input-group">
<input type="hidden" class="form-control" value="<?php echo htmlspecialchars($username); ?>" id="username" name="username">
<input type="text" class="form-control input-lg" placeholder="Post Comment" id="comment" name="comment">
<span class="input-group-btn">
<button class="btn btn-success btn-lg" type="button" id="comment-submit" name="comment-submit">Post</button>
</span>
</div>
</form>
</div>
</div>
</section>
loadComments.php
<?php
include('connection.php');
$custid = $_POST['custid'];
$sqlUpdate = "SELECT comdate, comuser, comment FROM commentstbl WHERE custid='".$custid."' ORDER BY comdate DESC";
$qryUpdate = mysqli_query($db,$sqlUpdate);
$comCntsql = "SELECT COUNT(comuser) AS totalcom FROM commentstbl WHERE custid='".$custid."'";
$comCntresult = mysqli_query($db,$comCntsql);
$comCount = mysqli_fetch_array($comCntresult,MYSQLI_ASSOC);
if ($comCount['totalcom'] > 0) {
while ($row = mysqli_fetch_array($qryUpdate)) {
$date = $row["comdate"];
$date = date("n/j/Y", strtotime($date));
$user = $row["comuser"];
$comment = $row["comment"];
$comments[] = array($user,$date,$comment);
}
} else {
$comments = 0;
}
$comLength = count($comments);
if ($comments != 0) {
for ($r=0;$r<$comLength;$r++) {
echo '<div class="panel panel-default comment"><div class="panel-heading">'.$comments[$r][0].'<div class="pull-right"><small>'.$comments[$r][1].'</small></div></div><div class="panel-wrapper collapse in" aria-expanded="true"><div class="panel-body"><p>'.$comments[$r][2].'</p></div></div></div>';
}
}
?>
saveComment.php
<?php
include('connection.php');
$custid = mysqli_real_escape_string($db, $_POST['custid']);
$postdate = date('Y-m-d H:i:s');
$poster = $_POST['username'];
$comment = mysqli_real_escape_string($db, $_POST['comment']);
$statement = $db->prepare("INSERT INTO commentstbl (custid, comdate, comuser, comment) VALUES (?,?,?,?)");
$statement->bind_param('isss', $custid, $postdate, $poster, $comment);
$statement->execute();
?>
以下是chrome开发人员工具的图片,显示当我点击#compm-提交按钮时它正在调用php文件两次:
我真的对这个问题感到茫然。如果有任何方向任何人都可以给我,我将不胜感激。如果有任何我遗漏的代码,请告诉我,如果有助于此过程,我很乐意提供。
答案 0 :(得分:2)
您触发该功能两次,因为您绑定了click
和touchstart
;)