Ajax上传未执行PHP脚本

Ajax上传未执行PHP脚本

本文介绍了Ajax上传未执行PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在chrome中通过控制台登录找到了我的错误并将其删除,但现在的问题是它没有调用应该处理数据的php脚本.

I have found my errors via console log in chrome and remove them but my problem now is that it doesn't call the php script that supposed to process the data.

<legend><strong>Select type of machine model</strong></legend>
<form id="form1" name="form1" method="post" enctype="multipart/form-data">
<select id="machine" name="machine" class="field" >
<option value="" selected="selected">Choose..</option>
<option value="machine1.php">Machine 1</option>
<option value="machine2.php">Machine 2</option>
</select>
</fieldset>
<fieldset>
    <legend><strong>Select a file to upload</strong></legend>
    <input type="file" id="files" name="files[]" size="40" multiple="multiple" />
     <br />
      <p></p>
       <input type="submit" value="Upload File" id="upload"/>
       <br />
        <br />
    </form>
    <div id="information">
    </div>
</fieldset>
<fieldset>
<legend><strong>Uploaded Files</strong></legend>
    <div id="uploaded"></div>
</fieldset>
<script type="text/javascript">
jQuery(document).ready(function(e){
jQuery('#upload').click(function(e){


    var formData = new FormData($('form')[0]);
    var page = $("#machine option:selected").val();

    //check the output here
    e.preventDefault();
    console.log("formData",formData)

    $.ajax({
        url: page,
        type: 'POST',
        data: formData,
        cache: false,
        contenType: false,
        processData: false,
        success: function(data){
            $("#information").empty();
            $("#information").append(data);
        }
    });
});

});

这正成为我的两难选择,现在我已经连续三天在Google搜索中,我发现了一些有用的代码,但是当我将其应用于我的代码时,即使没有错误,它也无法正常工作.预先感谢.

It's becoming my dilemma and I have been searching google for 3 straight days now and I found some useful codes but when I apply it to mine, it does not work even errors I have none. Thanks in advance.

我终于得到了我问题的答案,谢谢你给我的帮助.

I finally got the answer to my question, thanks for the help that you gave me..

$(document).ready(function(){
$("#form1").submit(function(e){
var formObj = $(this);
var page = $("#machine option:selected").val();

if(window.FormData !== undefined)
{
var formData = new FormData(this);

    $.ajax({
        url: page,
        type: 'POST',
        data: formData,
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function(data){
        $("#uploaded").empty();
        $("#uploaded").append(data);

}
});
e.preventDefault();

}
});

});

推荐答案

检查表单数据,这不是一个解决方案,但可以帮助您调试自己的代码.

Check your formdata, this is not a solution but will help you debug your own code.

<script type="text/javascript">
$(document).ready(function(e){
    $('#upload').click(function(e){


        var formData = new FormData($('form')[0]);
        var page = $("#machine option:selected").val();

        //check the output here
        e.preventDefault();
        console.log("formData",formData)

        $.ajax({
            url: page,
            type: 'POST',
            data: formData,
            cache: false,
            contenType: false,
            processData: false,
            success: function(data){
                $("#information").empty();
                $("#information").append(data);
            }
        });
    });
})

这篇关于Ajax上传未执行PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 15:28