douchen7366 2016-07-25 03:00
浏览 55

jQuery AJAX提交表单和文件

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.

  • 写回答

1条回答 默认 最新

  • doupang1917 2016-07-25 13:33
    关注

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

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析