我在点击触发器时弹出模式框的同一页面上有多个注释表单。我遇到的问题是,如果您在textarea中没有任何内容提交表单,则会出现错误,您需要输入注释,并且一切都很顺利。 Nowww ...如果你输入文本并正确填写并提交任何输入的文本将被提交失败的尝试次数,我不明白为什么。jquery表单提交两次
jquery的:
$(".add-comment")
.button()
.click(function() {
$(this).closest('.comments').find('.form-comment').submit(function() {
$('.com-error').hide();
$('.com-msg').hide();
$.post('/do-comment.php', $(this).serialize(),
function(data) {
if(data.success)
{
$('.com-msg').find('.info-msg').html(data.message);
$('.comments').find('.com-msg').slideDown().delay(2000).slideUp(500);
}
else
{
$('.com-error').find('.error-msg').html(data.message);
$('.com-error').slideDown().delay(2000).slideUp(500);
}
}, 'json');
return false;
});
});
在PHP
if (!$user->isAuthorized()) {
header('Location: index.php');
}else{
$i= 0;
$cmt = array();
if($_POST){
$cmt['bid'] = filter_input(INPUT_POST, 'bid', FILTER_SANITIZE_STRING);
$cmt['uid'] = $user->getId();
$cmt['msg'] = filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING);
if(trim($cmt['msg']) == '') {
$data['success'] = false;
$data['message'] .= 'You must enter a comment';
$i++;
}elseif(strlen($cmt['msg']) > 500) {
$data['success'] = false;
$data['message'] .= 'Your comment is to long.';
$i++;
}
if($i <= 0){
if(insertComment($cmt)){
$data['success'] = true;
$data['message'] = 'Added your comment!';
}else{
$data['success'] = false;
$data['message'] .= 'Error adding your comment';
}
}
echo json_encode($data);
}
unset($_POST);
}
上述
function insertComment($comment = array()) {
global $config;
$sql = "INSERT INTO comments (bid, uid, message) VALUES ('".mysql_real_escape_string($comment['bid'])."', ".mysql_real_escape_string($comment['uid']).", '".mysql_real_escape_string($comment['msg'])."')";
$result = mysql_query($sql, $config->db->con);
if(!$result) {
echo mysql_error();
return false;
}else{
return true;
}
}
形式使用的insertComment功能
<div class="comments">
<div class="wrap">
';
if(!$user->isAuthorized()){
$output .= '<center>You must be logged in to post comments</center>';
}else{
$output .= '
<form name="form-comment" class="form-comment" >
<textarea name="comment" class="beat-comment"></textarea>
<input type="hidden" name="bid" value="'.$id.'" />
<br />
<span class="fr"><button type="submit" class="add-comment">Add Comment</button></span>
</form>';
$output .= '
<div class="com-msg">
<div class="ui-widget">
<div class="ui-state-highlight ui-corner-all" style="padding: 0 .7em;">
<p>
<span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
<div class="info-msg">
<!-- MSG FROM AJAX REQUEST IF PRESENT-->
</div>
</p>
</div>
</div>
</div>
<div class="com-error">
<div class="ui-widget">
<div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
<p>
<span class="ui-icon ui-icon-alert" style="float: left; margin-right: .7em;"></span>
<div class="error-msg">
<!-- ERROR MSG FROM AJAX REQUEST IF PRESENT-->
</div>
</p>
</div>
</div>
</div>
';
}
个
感谢NeoNexus结合我们双方的一个解决方案
$(".add-comment")
.button()
.click(function() {
var myForm = $(this).parent();
$('.com-error').hide();
$('.com-msg').hide();
$.post('/do-comment.php', $(myForm).serialize(),
function(data) {
if(data.success)
{
$(myForm).parent().find('.info-msg').html(data.message);
$(myForm).parent().find('.com-msg').slideDown().delay(2000).slideUp(500);
}
else
{
$(myForm).parent().find('.error-msg').html(data.message);
$(myForm).parent().find('.com-error').slideDown().delay(2000).slideUp(500);
}
}, 'json');
return false;
});
不要编辑从答案添加代码,正好砸在大对号旁边正确的解决方案将其标记为答案。 :) – MetalFrog 2012-02-14 13:59:37
没问题Birgit_B,但是就像MetalFrog说的,如果你想感谢我,请点击答案旁边的大号复选标记。 – 2012-02-14 17:28:35