Php Wordpress表单,带文件上传和Ajax提交

Php Wordpress表单,带文件上传和Ajax提交,php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,我正在尝试创建表单并用ajax提交它。因此页面不会重新加载。我对ajax没有太多的经验,我正试图找到尽可能多的信息 现在在我的代码中,我可以在不重新加载的情况下提交表单。但我有一个上传文件的字段。我知道这样做有点不同,我也找到了一些例子,但到目前为止没有运气。比如说 现在我的Ajax代码如下所示: Ajax (function($) { jQuery(document).ready(function() { // when user submits the form jQue

我正在尝试创建表单并用ajax提交它。因此页面不会重新加载。我对ajax没有太多的经验,我正试图找到尽可能多的信息

现在在我的代码中,我可以在不重新加载的情况下提交表单。但我有一个上传文件的字段。我知道这样做有点不同,我也找到了一些例子,但到目前为止没有运气。比如说

现在我的Ajax代码如下所示:

Ajax

(function($) {

jQuery(document).ready(function() {

    // when user submits the form
    jQuery(document).on('submit', '.form-assignment', function(event) {

        var error_elm = jQuery('.ajax-error');
        var response_elm = jQuery('.ajax-response')
       // var widgetId = grecaptcha.reset(container);
        error_elm.html('');
        response_elm.html('');

        // prevent form submission
        event.preventDefault();

        var form_elm = jQuery(this);

        var url = form_elm.data('url');
        var action = form_elm.data('action');
        var form_data = new FormData();

        var data = {
            action: action,
            form_data : form_data
        };

        // add loading message
        response_elm.html('Loading...');

        jQuery.ajax({
            type : 'POST',
            url : url,
            data : data,
            processData: false,
            contentType: false,
            enctype: 'multipart/form-data',
            dataType : 'json',
            async : true
        }).success(function(response) {

            error_elm.html('');
            response_elm.html('');

            if(response.status !== 'success') {
                // something went wrong
                if(response.message) {
                    error_elm.html(response.message);
                    return;
                }

                // don't know ?
            }

            // success!!

            // log data
            console.log(response);

            // display data
            response_elm.html(response.message);
            $("#form-assignment").trigger("reset");
            grecaptcha.reset();
        }).error(function(response) {
            error_elm.html('');
            response_elm.html('');

            error_elm.html(response.statusText);
        });
    });

});



})( jQuery );
我的表格:

 <div class="ajax-error" style="color: red;"></div>
    <form class="form-assignment" name="form_assignment" id="form-assignment"
          method="post" enctype="multipart/form-data"
          data-url="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ) ?>" data-action="form_submit1">
          <label>name:</label>
          <input type="text" name="customer-field-text"pattern="[a-zA-Z0-9 ]+" placeholder="<?php echo $field->label ?>" size="40"/>
          <label>file upload</label>
           <input type="file"  name="customer-field-upload" id="customer-field-upload"
                           multiple="false"/>
    </form>
jQuery(document).ready(function ($) {
    // when user submits the form
    jQuery(document).on('submit', '.form-assignment', function (event) {

        var error_elm = jQuery('.ajax-error');
        var response_elm = jQuery('.ajax-response');
        error_elm.html('');
        response_elm.html('');

        event.preventDefault();

        var form_elm = jQuery(this);

        var url = form_elm.data('url');
        var action = form_elm.data('action');
        var file_input_name = jQuery('#form-assignment').find('input[type=file]').attr('id');
        var form_data = new FormData();

        form_data.append('action', action);

        jQuery.each(jQuery(':input:not([type=submit]):not([type=file])', '#form-assignment' ), function(i, fileds){
            form_data.append(jQuery(fileds).attr('name'), jQuery(fileds).val());
        });
        jQuery.each(jQuery(':input:not([type=submit]):not([type=text]):not([type=select])', '#form-assignment' )[0].files, function(i, file){
            form_data.append(file_input_name, file);
        });

        response_elm.html('Loading...');

        jQuery.ajax({
            type: 'POST',
            url: url,
            data: form_data,
            processData: false,
            contentType: false,
            cache: false
        }).success(function (response) {

            error_elm.html('');
            response_elm.html('');

            if (response.status !== 'success') {
                // something went wrong
                if (response.message) {
                    error_elm.html(response.message);
                    return;
                }

                // don't know ?
            }

            response_elm.html(response.message);
            $("#form-assignment").trigger("reset");

        }).error(function (response) {
            error_elm.html('');
            response_elm.html('');

            error_elm.html(response.statusText);
        });
    });

});


您缺少
enctype:'multipart/form data'
contentType:false
,以防止jQuery将其设置为字符串。如果这不起作用,请查看此内容并遵循相同的方法

   jQuery.ajax({
        type : 'POST',
        enctype: 'multipart/form-data',
        url : url,
        data : data,
        async : true,
        processData: false,
        contentType: false,
    })

您缺少
enctype:'multipart/form data'
contentType:false
,以防止jQuery将其设置为字符串。如果这不起作用,请查看此内容并遵循相同的方法

   jQuery.ajax({
        type : 'POST',
        enctype: 'multipart/form-data',
        url : url,
        data : data,
        async : true,
        processData: false,
        contentType: false,
    })

您的Jquery缺少

enctype: 'multipart/form-data'
这在您提交表格时非常重要。这允许在不进行任何转换的情况下发送表单数据

jQuery.ajax({
            type : 'POST',
            url : url,
            data : data,
            enctype: 'multipart/form-data',
            dataType : 'json',
            async : true
        }).success(function(response) {

            error_elm.html('');
            response_elm.html('');

            if(response.status !== 'success') {
                // something went wrong
                if(response.message) {
                    error_elm.html(response.message);
                    return;
                }

                // don't know ?
            }

            // success!!

            // log data
            console.log(response);

            // display data
            response_elm.html(response.message);
        }).error(function(response) {
            error_elm.html('');
            response_elm.html('');

            error_elm.html(response.statusText);
        });
    });

您的Jquery缺少

enctype: 'multipart/form-data'
这在您提交表格时非常重要。这允许在不进行任何转换的情况下发送表单数据

jQuery.ajax({
            type : 'POST',
            url : url,
            data : data,
            enctype: 'multipart/form-data',
            dataType : 'json',
            async : true
        }).success(function(response) {

            error_elm.html('');
            response_elm.html('');

            if(response.status !== 'success') {
                // something went wrong
                if(response.message) {
                    error_elm.html(response.message);
                    return;
                }

                // don't know ?
            }

            // success!!

            // log data
            console.log(response);

            // display data
            response_elm.html(response.message);
        }).error(function(response) {
            error_elm.html('');
            response_elm.html('');

            error_elm.html(response.statusText);
        });
    });

以下是一些让工作成为您所提供表单的提示:

表格

<div class="ajax-error" style="color: red;"></div>
<form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
      data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
    <label>name:</label>
    <input type="text" name="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
    <label>file upload</label>
    <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
    <input type="submit" value="Submit" name="submit">
</form>
jQuery(document).ready(function ($) {
    // when user submits the form
    jQuery(document).on('submit', '.form-assignment', function (event) {

        var error_elm = jQuery('.ajax-error');
        var response_elm = jQuery('.ajax-response');
        error_elm.html('');
        response_elm.html('');

        event.preventDefault();

        var form_elm = jQuery(this);

        var url = form_elm.data('url');
        var action = form_elm.data('action');
        var file = form_elm[0][1].files[0];
        var customer_field_text = form_elm[0][0].value;
        var form_data = new FormData();
        form_data.append('action', action);
        form_data.append('customer-field-upload', file);
        form_data.append('customer-field-name', customer_field_text);

        response_elm.html('Loading...');

        jQuery.ajax({
            type: 'POST',
            url: url,
            data: form_data,
            processData: false,
            contentType: false,
            cache: false
        }).success(function (response) {

            error_elm.html('');
            response_elm.html('');

            if (response.status !== 'success') {
                // something went wrong
                if (response.message) {
                    error_elm.html(response.message);
                    return;
                }

                // don't know ?
            }

            response_elm.html(response.message);
            $("#form-assignment").trigger("reset");

        }).error(function (response) {
            error_elm.html('');
            response_elm.html('');

            error_elm.html(response.statusText);
        });
    });

});
<div class="ajax-error" style="color: red;"></div>
<form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
      data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
    <label for="customer-field-text">name:</label>
    <input type="text" name="customer-field-text" id="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
    <label>file upload</label>
    <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
    <label for="select">select:</label>
    <select name="carlist" id="select">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
    <label for="email">email: </label>
    <input type="text" name="email" id="email">
    <input type="submit" value="Submit" name="submit">
</form>
注意:您使用的是
var form\u elm=jQuery(这个),返回包含所有表单内容的jquery对象。这就是为什么我用它来访问输入信息。相反,您可以使用表单输入的名称、类、id、占位符等来访问表单输入

发生了什么变化

<div class="ajax-error" style="color: red;"></div>
<form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
      data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
    <label>name:</label>
    <input type="text" name="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
    <label>file upload</label>
    <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
    <input type="submit" value="Submit" name="submit">
</form>
jQuery(document).ready(function ($) {
    // when user submits the form
    jQuery(document).on('submit', '.form-assignment', function (event) {

        var error_elm = jQuery('.ajax-error');
        var response_elm = jQuery('.ajax-response');
        error_elm.html('');
        response_elm.html('');

        event.preventDefault();

        var form_elm = jQuery(this);

        var url = form_elm.data('url');
        var action = form_elm.data('action');
        var file = form_elm[0][1].files[0];
        var customer_field_text = form_elm[0][0].value;
        var form_data = new FormData();
        form_data.append('action', action);
        form_data.append('customer-field-upload', file);
        form_data.append('customer-field-name', customer_field_text);

        response_elm.html('Loading...');

        jQuery.ajax({
            type: 'POST',
            url: url,
            data: form_data,
            processData: false,
            contentType: false,
            cache: false
        }).success(function (response) {

            error_elm.html('');
            response_elm.html('');

            if (response.status !== 'success') {
                // something went wrong
                if (response.message) {
                    error_elm.html(response.message);
                    return;
                }

                // don't know ?
            }

            response_elm.html(response.message);
            $("#form-assignment").trigger("reset");

        }).error(function (response) {
            error_elm.html('');
            response_elm.html('');

            error_elm.html(response.statusText);
        });
    });

});
<div class="ajax-error" style="color: red;"></div>
<form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
      data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
    <label for="customer-field-text">name:</label>
    <input type="text" name="customer-field-text" id="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
    <label>file upload</label>
    <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
    <label for="select">select:</label>
    <select name="carlist" id="select">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
    <label for="email">email: </label>
    <input type="text" name="email" id="email">
    <input type="submit" value="Submit" name="submit">
</form>
  • 删除了
    (函数($){}
    包装器,而使用了
    jQuery(document).ready(函数($){});
    。这只是为了说明。无需包装jQuery即可访问
    $
  • 我们从以下行输入中获取文件和名称:

    var file = form_elm[0][1].files[0];
    var customer_field_text = form_elm[0][0].value;
    
  • 我们正在为WordPress添加
    action
    ,并将其值形式输入到:

    在这里,您可以更改我们正在发送的阵列的名称。例如,我们将使用key
    customer field upload
    接收阵列中的文件,并可以这样使用它:

    $_FILES['customer-field-upload']
    
  • async:true
    :这一行我们不需要。它是。
    enctype:'multipart/form data',
    我们也不需要
  • data:form\u data,
    我们只使用一个
    form\u data
    变量(
    FormData
    对象)发送所有内容
  • PHP文件:

  • 我没有使用
    add\u action('wp\uajax\unopriv\u form\u submit1','handle\u form\u submit');
    ,因为在您提供的代码中没有与
    nonce
    相关的内容
  • 您的
    $\u帖子将包含:

    Array
    (
       [action] => form_submit1 //action, which we used to send and accept Ajax request
       [customer-field-name] => some input value in the name field
    )
    
    Array
    (
        [customer-field-upload] => Array
            (
                [name] => input file name
                [type] => input file type
                [tmp_name] => temp file
                [error] => 0 
                [size] => some size
            )
    
    )
    
    您可以使用
    $\u POST['customer-field-name']
    访问名称输入

  • 您的
    $\u文件将包含:

    Array
    (
       [action] => form_submit1 //action, which we used to send and accept Ajax request
       [customer-field-name] => some input value in the name field
    )
    
    Array
    (
        [customer-field-upload] => Array
            (
                [name] => input file name
                [type] => input file type
                [tmp_name] => temp file
                [error] => 0 
                [size] => some size
            )
    
    )
    
    您可以使用
    $\u文件['customer-field-upload']


  • 编辑:附加函数以添加带有多行代码的所有输入:

    表格

    <div class="ajax-error" style="color: red;"></div>
    <form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
          data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
        <label>name:</label>
        <input type="text" name="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
        <label>file upload</label>
        <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
        <input type="submit" value="Submit" name="submit">
    </form>
    
    jQuery(document).ready(function ($) {
        // when user submits the form
        jQuery(document).on('submit', '.form-assignment', function (event) {
    
            var error_elm = jQuery('.ajax-error');
            var response_elm = jQuery('.ajax-response');
            error_elm.html('');
            response_elm.html('');
    
            event.preventDefault();
    
            var form_elm = jQuery(this);
    
            var url = form_elm.data('url');
            var action = form_elm.data('action');
            var file = form_elm[0][1].files[0];
            var customer_field_text = form_elm[0][0].value;
            var form_data = new FormData();
            form_data.append('action', action);
            form_data.append('customer-field-upload', file);
            form_data.append('customer-field-name', customer_field_text);
    
            response_elm.html('Loading...');
    
            jQuery.ajax({
                type: 'POST',
                url: url,
                data: form_data,
                processData: false,
                contentType: false,
                cache: false
            }).success(function (response) {
    
                error_elm.html('');
                response_elm.html('');
    
                if (response.status !== 'success') {
                    // something went wrong
                    if (response.message) {
                        error_elm.html(response.message);
                        return;
                    }
    
                    // don't know ?
                }
    
                response_elm.html(response.message);
                $("#form-assignment").trigger("reset");
    
            }).error(function (response) {
                error_elm.html('');
                response_elm.html('');
    
                error_elm.html(response.statusText);
            });
        });
    
    });
    
    <div class="ajax-error" style="color: red;"></div>
    <form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
          data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
        <label for="customer-field-text">name:</label>
        <input type="text" name="customer-field-text" id="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
        <label>file upload</label>
        <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
        <label for="select">select:</label>
        <select name="carlist" id="select">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>
        <label for="email">email: </label>
        <input type="text" name="email" id="email">
        <input type="submit" value="Submit" name="submit">
    </form>
    

    在这里,我们用于将循环中的几个值添加到
    FormData
    对象中。这是一个示例,可用于复选框、文本区域等。

    以下是使用提供的表单的一些技巧:

    窗体

    <div class="ajax-error" style="color: red;"></div>
    <form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
          data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
        <label>name:</label>
        <input type="text" name="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
        <label>file upload</label>
        <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
        <input type="submit" value="Submit" name="submit">
    </form>
    
    jQuery(document).ready(function ($) {
        // when user submits the form
        jQuery(document).on('submit', '.form-assignment', function (event) {
    
            var error_elm = jQuery('.ajax-error');
            var response_elm = jQuery('.ajax-response');
            error_elm.html('');
            response_elm.html('');
    
            event.preventDefault();
    
            var form_elm = jQuery(this);
    
            var url = form_elm.data('url');
            var action = form_elm.data('action');
            var file = form_elm[0][1].files[0];
            var customer_field_text = form_elm[0][0].value;
            var form_data = new FormData();
            form_data.append('action', action);
            form_data.append('customer-field-upload', file);
            form_data.append('customer-field-name', customer_field_text);
    
            response_elm.html('Loading...');
    
            jQuery.ajax({
                type: 'POST',
                url: url,
                data: form_data,
                processData: false,
                contentType: false,
                cache: false
            }).success(function (response) {
    
                error_elm.html('');
                response_elm.html('');
    
                if (response.status !== 'success') {
                    // something went wrong
                    if (response.message) {
                        error_elm.html(response.message);
                        return;
                    }
    
                    // don't know ?
                }
    
                response_elm.html(response.message);
                $("#form-assignment").trigger("reset");
    
            }).error(function (response) {
                error_elm.html('');
                response_elm.html('');
    
                error_elm.html(response.statusText);
            });
        });
    
    });
    
    <div class="ajax-error" style="color: red;"></div>
    <form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
          data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
        <label for="customer-field-text">name:</label>
        <input type="text" name="customer-field-text" id="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
        <label>file upload</label>
        <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
        <label for="select">select:</label>
        <select name="carlist" id="select">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>
        <label for="email">email: </label>
        <input type="text" name="email" id="email">
        <input type="submit" value="Submit" name="submit">
    </form>
    
    注意:您使用的是
    var form_elm=jQuery(this);
    ,它返回jQuery的对象以及所有表单内容。这就是为什么我使用它来访问输入信息。相反,您可以使用名称、类、id、占位符等来访问表单输入

    发生了什么变化

    <div class="ajax-error" style="color: red;"></div>
    <form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
          data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
        <label>name:</label>
        <input type="text" name="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
        <label>file upload</label>
        <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
        <input type="submit" value="Submit" name="submit">
    </form>
    
    jQuery(document).ready(function ($) {
        // when user submits the form
        jQuery(document).on('submit', '.form-assignment', function (event) {
    
            var error_elm = jQuery('.ajax-error');
            var response_elm = jQuery('.ajax-response');
            error_elm.html('');
            response_elm.html('');
    
            event.preventDefault();
    
            var form_elm = jQuery(this);
    
            var url = form_elm.data('url');
            var action = form_elm.data('action');
            var file = form_elm[0][1].files[0];
            var customer_field_text = form_elm[0][0].value;
            var form_data = new FormData();
            form_data.append('action', action);
            form_data.append('customer-field-upload', file);
            form_data.append('customer-field-name', customer_field_text);
    
            response_elm.html('Loading...');
    
            jQuery.ajax({
                type: 'POST',
                url: url,
                data: form_data,
                processData: false,
                contentType: false,
                cache: false
            }).success(function (response) {
    
                error_elm.html('');
                response_elm.html('');
    
                if (response.status !== 'success') {
                    // something went wrong
                    if (response.message) {
                        error_elm.html(response.message);
                        return;
                    }
    
                    // don't know ?
                }
    
                response_elm.html(response.message);
                $("#form-assignment").trigger("reset");
    
            }).error(function (response) {
                error_elm.html('');
                response_elm.html('');
    
                error_elm.html(response.statusText);
            });
        });
    
    });
    
    <div class="ajax-error" style="color: red;"></div>
    <form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
          data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
        <label for="customer-field-text">name:</label>
        <input type="text" name="customer-field-text" id="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
        <label>file upload</label>
        <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
        <label for="select">select:</label>
        <select name="carlist" id="select">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>
        <label for="email">email: </label>
        <input type="text" name="email" id="email">
        <input type="submit" value="Submit" name="submit">
    </form>
    
  • 删除了
    (函数($){}
    包装器,而使用了
    jQuery(document).ready(函数($){});
    。这只是为了说明。无需包装jQuery即可访问
    $
  • 我们正在从输入中获取文件和名称,其中有以下行:

    var file = form_elm[0][1].files[0];
    var customer_field_text = form_elm[0][0].value;
    
  • 我们正在为WordPress添加
    action
    ,并将其值形式输入到:

    在这里,您可以更改我们正在发送的阵列的名称。例如,我们将使用key
    customer field upload
    接收阵列中的文件,并可以这样使用它:

    $_FILES['customer-field-upload']
    
  • async:true
    :这一行我们不需要。它是。
    enctype:'multipart/form data',
    我们也不需要
  • data:form\u data,
    我们只使用一个
    form\u data
    变量(
    FormData
    对象)发送所有内容
  • PHP文件:

  • 我没有使用
    add\u action('wp\uajax\unopriv\u form\u submit1','handle\u form\u submit');
    ,因为在您提供的代码中没有与
    nonce
    相关的内容
  • 您的
    $\u帖子将包含:

    Array
    (
       [action] => form_submit1 //action, which we used to send and accept Ajax request
       [customer-field-name] => some input value in the name field
    )
    
    Array
    (
        [customer-field-upload] => Array
            (
                [name] => input file name
                [type] => input file type
                [tmp_name] => temp file
                [error] => 0 
                [size] => some size
            )
    
    )
    
    您可以使用
    $\u POST['customer-field-name']
    访问名称输入

  • 您的
    $\u文件将包含:

    Array
    (
       [action] => form_submit1 //action, which we used to send and accept Ajax request
       [customer-field-name] => some input value in the name field
    )
    
    Array
    (
        [customer-field-upload] => Array
            (
                [name] => input file name
                [type] => input file type
                [tmp_name] => temp file
                [error] => 0 
                [size] => some size
            )
    
    )
    
    您可以使用
    $\u文件['customer-field-upload']


  • 编辑:附加函数以添加带有多行代码的所有输入:

    表格

    <div class="ajax-error" style="color: red;"></div>
    <form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
          data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
        <label>name:</label>
        <input type="text" name="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
        <label>file upload</label>
        <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
        <input type="submit" value="Submit" name="submit">
    </form>
    
    jQuery(document).ready(function ($) {
        // when user submits the form
        jQuery(document).on('submit', '.form-assignment', function (event) {
    
            var error_elm = jQuery('.ajax-error');
            var response_elm = jQuery('.ajax-response');
            error_elm.html('');
            response_elm.html('');
    
            event.preventDefault();
    
            var form_elm = jQuery(this);
    
            var url = form_elm.data('url');
            var action = form_elm.data('action');
            var file = form_elm[0][1].files[0];
            var customer_field_text = form_elm[0][0].value;
            var form_data = new FormData();
            form_data.append('action', action);
            form_data.append('customer-field-upload', file);
            form_data.append('customer-field-name', customer_field_text);
    
            response_elm.html('Loading...');
    
            jQuery.ajax({
                type: 'POST',
                url: url,
                data: form_data,
                processData: false,
                contentType: false,
                cache: false
            }).success(function (response) {
    
                error_elm.html('');
                response_elm.html('');
    
                if (response.status !== 'success') {
                    // something went wrong
                    if (response.message) {
                        error_elm.html(response.message);
                        return;
                    }
    
                    // don't know ?
                }
    
                response_elm.html(response.message);
                $("#form-assignment").trigger("reset");
    
            }).error(function (response) {
                error_elm.html('');
                response_elm.html('');
    
                error_elm.html(response.statusText);
            });
        });
    
    });
    
    <div class="ajax-error" style="color: red;"></div>
    <form class="form-assignment" name="form_assignment" id="form-assignment" method="post" enctype="multipart/form-data"
          data-url="<?php echo esc_url(admin_url('admin-ajax.php')) ?>" data-action="form_submit1">
        <label for="customer-field-text">name:</label>
        <input type="text" name="customer-field-text" id="customer-field-text" pattern="[a-zA-Z0-9 ]+" size="40"/>
        <label>file upload</label>
        <input type="file" name="customer-field-upload" id="customer-field-upload" multiple="false"/>
        <label for="select">select:</label>
        <select name="carlist" id="select">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>
        <label for="email">email: </label>
        <input type="text" name="email" id="email">
        <input type="submit" value="Submit" name="submit">
    </form>
    

    在这里,我们曾经将循环中的几个值添加到
    FormData
    对象中。这是一个示例,可以应用于复选框、文本区域等。

    非常感谢您的回复,我不知道我怎么会忘记。但这并没有解决问题。当我选择一个文件时,表单没有看到同样的问题。提交表单时返回我还没有选择一个文件。好吧,让我为你的问题做另一个测试用例,然后尽快回复你。我发现了一个很小的情况。这是通过使用表单数据解决的。但是我不知道如何在php中处理表单,因为我返回了json响应。所以我无法用这种方式解决。有关于测试用例的信息吗?非常感谢你的回复我不知道我该怎么做