简体   繁体   English

jQuery AJAX提交带有文件的表单

[英]jQuery AJAX submit form with file

It's in WordPress. 在WordPress中。 I have to submit form with file and get that file. 我必须提交带有文件的表格并获取该文件。 I'm getting the text fields data very well, but unable to catch file :( there is my code below. 我的文本字段数据很好,但无法捕获文件:(下面是我的代码。

HTML 的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>

There is my jQuery and AJAX code 有我的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){
        }
    });
});

I there is PHP code which i was try already. 我有我已经尝试过的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>";
}

If any solution for this please post your answer. 如果对此有任何解决方案,请发表您的答案。

You have to change following Code. 您必须更改以下代码。 It's Working perfect. 工作完美。

jQuery and AJAX code 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 Code 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