jQuery Ajax Post - 表单数据未正确提交


jQuery Ajax Post - Form data is not submitted correctly

我有这个表格

 <form method="post" name="addreply" id="addreply"> 
                <input type="hidden" name="pmid" id="pmid" value="">
                <textarea rows="10" cols="5" name="message" id="message" placeholder="Write your message..."></textarea>
                <input type="submit" class="butt green" value="Send">
              </form>

这是提交表单时调用的 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();
        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();

应该在哪里使用:

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); 

        }

我似乎找不到问题所在。谁能帮我?

您需要修剪数据,它很可能有一些尾随空格:

data = $.trim(data);

 data = data.trim();

为此使用 ajax,这是非常优雅的方法

$("#addreply").submit(function(event){
    event.preventDefault();
    $.ajax({
            url:"abc.php",
            type:"POST",
            data:$(this).serialize(),
            success: function(data){
                data=data.trim();
                alert(data);
            },
            error: function(data){
                data=data.trim();
                alert(data);
            }
            });
});