0

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.

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

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.

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.

Feroz Ahmed
  • 931
  • 10
  • 16
  • 1
    Try posting file appended to `FormData` object, see http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously?s=1|10.9107 – guest271314 Jul 25 '16 at 03:03

1 Answers1

0

You have to change following Code. It's Working perfect.

jQuery and AJAX code

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;
?>