我在这个问题上读到了类似的问题,但我无法找到它为什么会这样做。 基本上jquery ajax调用会发送一些要提交的信息。问题是表格第一次提交,第二次提交两次,第三次提交4次。我无法理解问题出在哪里。你能帮忙吗?
$('#form10').submit(function () {
//Get the data from all the fields
var UserID = $('input[name=UserID]');
var problemID = $('input[name=problemID]');
var solution_text = $('textarea[name=solution_text]');
var MM_insert = $('input[name=MM_insert]');
//organize the data properly
var data = 'UserID=' + UserID.val() + '&problemID=' + problemID.val() + '&solution_text=' + solution_text.val() + '&MM_insert=' + MM_insert.val();
//show the loading sign
$('.loading').show().delay(1200).fadeOut();
//start the ajax
$.ajax({
//this is the php file that processes the data
url: "/js/ajax/add-comment.php",
//POST method is used
type: "POST",
//pass the data
data: data,
//Do not cache the page
cache: false,
//success
success: function (html) {
//if process.php returned 1/true (success)
if (html == 1) {
//hide the gif
$('.loading').fadeOut('slow');
//show the success message
$('#comment-success').delay(1000).slideDown('slow').delay(2000).slideUp('slow');
$('#comments').load('/js/ajax/comments2.php?problemID=' + problemID.val());
//if process.php returned 0/false (failed)
} else {
$('#error-msg2').show('slow');
}
}
});
//cancel the submit button default behaviours
return false;
});
php返回代码
.............
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "comments")) {
$insertSQL = sprintf("INSERT INTO solution (UserID, solution_date, problemID, solution_text) VALUES (%s, %s, %s, %s)",
GetSQLValueString($UserID, "int"),
GetSQLValueString(date('Y-m-d H:i:s', mktime()), "text"),
GetSQLValueString($problemID, "int"),
GetSQLValueString($solution_text, "text"));
mysql_select_db($database_tccconn, $tccconn);
$Result1 = mysql_query($insertSQL, $tccconn) or die(mysql_error());
if($Result1) { echo '1';}
else
{ echo '0';}
}
表格
form method="post" action="" name="form10" id="form10">
<input type="hidden" name="problemID" value="<?php echo $this->escape($this->item['problemID']);?>">
<input type="hidden" name="UserID" value="<?php $identity = Zend_Auth::getInstance()->getIdentity(); ?>">
<textarea name="solution_text" id="solution_text" class="create" rows="6"></textarea>
<input type="submit" name="submit" value="Reply to post" class="submit">
<input type="hidden" name="MM_insert" value="comments">
<div class="loading"></div>
</form>
code frm comments2
mysql_select_db($database_tccconn, $tccconn);
$query_rstcomments = sprintf("SELECT solution.solutionID, solution.UserID, solution.solution_date, solution.solution_text, solution.problemID, user.org_name FROM solution NATURAL JOIN user WHERE solution.problemID = %s ORDER BY solution.solutionID ASC", $colname_rstcomments);
$rstcomments = mysql_query($query_rstcomments, $tccconn) or die(mysql_error());
$row_rstcomments = mysql_fetch_assoc($rstcomments);
$totalRows_rstcomments = mysql_num_rows($rstcomments);
if($rstcomments) {
do { ?>
<div class="comments-in-top">
<div style="width: 166px; float: left">
<div class="comments-in-bottom">
Posted by<br /><strong><?php echo ucwords(stripslashes($row_rstcomments['org_name'])); ?></strong><br />On <?php echo date("j F Y",strtotime($row_rstcomments['solution_date'])); ?><br />at <?php echo date("H:i",strtotime($row_rstcomments['solution_date'])); ?>
</div>
<div class="comments-in-bottom-b"></div>
</div>
<div class="comments-in-top-in">
<?php echo nl2br(ucfirst(stripslashes($row_rstcomments['solution_text']))); ?>
</div>
</div>
<?php } while ($row_rstcomments = mysql_fetch_assoc($rstcomments));
} ?>
答案 0 :(得分:3)
提交表单时使用unbind('submit')函数。这应该解决它 (除以下代码中的第1行外没有变化)
$('#form10').unbind('submit').submit(function () {
//Get the data from all the fields
var UserID = $('input[name=UserID]');
var problemID = $('input[name=problemID]');
var solution_text = $('textarea[name=solution_text]');
var MM_insert = $('input[name=MM_insert]');
//organize the data properly
var data = 'UserID=' + UserID.val() + '&problemID=' + problemID.val() + '&solution_text=' + solution_text.val() + '&MM_insert=' + MM_insert.val();
//show the loading sign
$('.loading').show().delay(1200).fadeOut();
//start the ajax
$.ajax({
//this is the php file that processes the data
url: "/js/ajax/add-comment.php",
//POST method is used
type: "POST",
//pass the data
data: data,
//Do not cache the page
cache: false,
//success
success: function (html) {
//if process.php returned 1/true (success)
if (html == 1) {
//hide the gif
$('.loading').fadeOut('slow');
//show the success message
$('#comment-success').delay(1000).slideDown('slow').delay(2000).slideUp('slow');
$('#comments').load('/js/ajax/comments2.php?problemID=' + problemID.val());
//if process.php returned 0/false (failed)
} else {
$('#error-msg2').show('slow');
}
}
});
//cancel the submit button default behaviours
return false;
});