我正在使用 jQuery 提交表单
像这样:
$(function() {
$('#addreply').submit(function(){
$('#status').removeClass().addClass('alert info').html('Loading...').fadeIn();
$.post(
'/index.php?i=pm&p=r',
$('form').serialize(),
function (data) {
proccessData(data);
}
);
return false;
});
});
function proccessData (data) {
$('#status').hide().html('');
if(data=='success'){
$("#post").append('<li class="current-user"><img width="30" height="30" src="<?php echo $userdata['avatar'] ?>"><div class="bubble"><a class="user-name"><?php echo $userdata['username']; ?></a><p class="message">'+ $("#message").val() +'</p><p class="time"></p></div></li>');
$('#message').val('');
$(".widget-content").animate({ scrollTop: $('.widget-content')[0].scrollHeight}, 1000);
}
else {
$('#status').removeClass().addClass('alert error').html(data).fadeIn();
}
}
这是PHP:
if($_POST)
{
$replyPrivateMessage = $privateMessage->replyMessage();
/*
* Error codes:
* 1 - You do not own this pm.
* 2 - No text entered.
* 3 - Text is too short.
* 4 - Text is too long.
* 5 - You have reached your PM limit.
* 6 - Error in adding your reply. Unexpected error.
* Success code:
* 100 - Successfully updated.
*
*/
switch($replyPrivateMessage)
{
case 1:
$error = 'The entered text is either too short or too long.';
$stop = true;
break;
case 2:
$error = 'Unexpected error.';
$stop = true;
break;
//If no error = success.
case 100:
die('success');
break;
}
die($error);
}
问题是当我提交表单时(并且没有错误),它会打印出“成功”,但它会使用这个:
$('#status').removeClass().addClass('alert error').html(data).fadeIn();
因为我可以看到 div 有 error alert 错误。因此,它打印出“成功”并正确运行表单 - 插入数据库和所有。
我的问题是,为什么不这样做:
if(data=='success'){
$("#post").append('<li class="current-user"><img width="30" height="30" src="<?php echo $userdata['avatar'] ?>"><div class="bubble"><a class="user-name"><?php echo $userdata['username']; ?></a><p class="message">'+ $("#message").val() +'</p><p class="time"></p></div></li>');
$('#message').val('');
$(".widget-content").animate({ scrollTop: $('.widget-content')[0].scrollHeight}, 1000);
}
既然收到的数据就是“成功”?