簡體   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