簡體   English   中英

jQuery AJAX提交帶有文件的表單

[英]jQuery AJAX submit form with file

在WordPress中。 我必須提交帶有文件的表格並獲取該文件。 我的文本字段數據很好,但無法捕獲文件:(下面是我的代碼。

的HTML

<form method="POST" class="JsFormPro" enctype="multipart/form-data">
    <table>
        <tr>
            <td>
                <textarea name="WordText" id="WordText" class="WordText" cols="30" rows="10"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <input type="file" id="WordFile" class="WordFile" name="WordFile">
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="" value="Submit">
            </td>
        </tr>
    </table>
</form>

有我的jQuery和AJAX代碼

var form = $('.JsFormPro');
form.on('submit', function(e){
    e.preventDefault();
    $.ajax({
        dataType : 'json',
        type: "POST",
        url: 'hit.php',
        data : {action : 'count_word_prism', Post : form.serialize()},

        beforeSend: function(){
        },

        success: function(Response){
        }
    });
});

我有我已經嘗試過的PHP代碼。

extract($_POST);
if ($Post) {
    parse_str($Post, $get_array);
    echo"<PRE>";
    print_r($get_array['WordText']);
    echo"</PRE>";

    echo"<PRE>";
    print_r($_FILES['WordFile']);
    echo"</PRE>";
}

如果對此有任何解決方案,請發表您的答案。

您必須更改以下代碼。 工作完美。

jQuery和AJAX代碼

var form = $('.JsFormPro');
form.on('submit', function(e) {
    var formData = new FormData(this);
    formData.append('action', 'count_word_prism');
    e.preventDefault();
    $.ajax({
        dataType : 'json',
        url: "hit.php", 
        type: "POST", 
        data: formData, 
        contentType: false,
        cache: false,
        processData: false,
        beforeSend: function(){
        },
        success: function(Response){
        }
    });
})

PHP代碼

<?php
echo "<pre>";
print_r($_POST);
print_r($_FILES);
die;
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM