繁体   English   中英

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM