jQuery AJAX提交表单和文件

时间:2022-11-24 12:29:58

It's in 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.

它在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>

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.

如果有任何解决方案,请发布您的答案。

1 个解决方案

#1


0  

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
echo "<pre>";
print_r($_POST);
print_r($_FILES);
die;
?>

#1


0  

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
echo "<pre>";
print_r($_POST);
print_r($_FILES);
die;
?>