ajax提交整个表单包含file类型文件

表单

<form class="center with-shadow indent-on-large-phone" id="new_entry" accept-charset="UTF-8" method="post" enctype="multipart/form-data">
    <div class="field field-text-field col-sm-12 required" data-api-code="field_8" data-type="TextField" data-label="需要投诉的枝数是?" data-validations="[&quot;Presence&quot;]">
        <div class="form-group">
            <div class="field-label-container" onclick="">
                <label class="field-label" for="entry_field_8">
                    需要投诉的枝数是?
                </label>
            </div>
            <div class="field-content">
                <input type="text" name="entry[field_8]" id="entry_field_8">
            </div>
        </div>
    </div>
    <div class="field field-check-box col-sm-12 required" data-api-code="field_9" data-type="CheckBox" data-label="主要投诉原因" data-validations="[&quot;Presence&quot;]">
        <div class="form-group">
            <div class="field-label-container" onclick="">
                <label class="field-label" for="entry_field_9">
                    主要投诉原因
                </label>
            </div>
            <div class="field-content">
                <div class="choices">
                    <label onclick="" class="checkbox ">
                        <input type="checkbox" value="打包损坏/断头" name="entry[field_9][]">
                        <div class="choice-description">
                            打包损坏/断头
                        </div>
                    </label>
                    <label onclick="" class="checkbox ">
                        <input type="checkbox" value="病虫害/如灰霉,霜霉" name="entry[field_9][]">
                        <div class="choice-description">
                            病虫害/如灰霉,霜霉
                        </div>
                    </label>
                    <label onclick="" class="checkbox ">
                        <input type="checkbox" value=" 严重脱水" name="entry[field_9][]">
                        <div class="choice-description">
                            严重脱水
                        </div>
                    </label>
                    <div class="other-choice-area ">
                        <label onclick="" class="checkbox  ">
                            <input class="other_choice" data-field-key="field_9" type="checkbox" value="其他" name="entry[field_9][]">
                            <div class="choice-description">
                                其他
                            </div>
                        </label>
                        <span class="other-choice-input-container"><input class="other-choice-input gd-input-medium gd-input-thin fixed-width-control" data-field-key="field_9" data-label="其他" type="text" value="" name="entry[field_9_other]" id="entry_field_9_other"></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <input type="file" name="img2[]" style="opacity: 1;    position: inherit;">
    <input type="file" name="img2[]" style="opacity: 1;    position: inherit">
    <input type="file" name="img2[]" style="opacity: 1;    position: inherit">
    <input type="file" name="img2[]" style="opacity: 1;    position: inherit">
    <input type="file" name="img2[]" style="opacity: 1;    position: inherit">
</form>

js 最后添加

    $("#new_entry").submit(function(event) {
        event.preventDefault();
        var formData = new FormData($("#new_entry")[0]);  
        $.ajax({
          url: 'http://m.luhong421.com/apprun/apprun/show_back_com_form',
          type: 'POST',
          dataType: 'json',
          async: false,
          data: formData,
          cache: false,  
                    contentType: false,  
                    processData: false,//好像不可缺!
          complete: function(xhr, textStatus) {
            //called when complete
          },
          success: function(data, textStatus, xhr) {
            layer.open({content:data.msg,time:2});
            if (data.code==1) {
                window.location.reload()
            }else{

            }
          },
          error: function(xhr, textStatus, errorThrown) {
            layer.open({content:"网络错误!",time:2});
          }
        });
    });

php处理

public function show_back_com_form()
    {
        is_crossdomain_app(true);
        $entry                = $_POST['entry'];
        $entry_field_10_files = input('entry_field_10_files');
        $entry_field_11_files = input('entry_field_11_files');
        $data                 = [
            'uid'           => cookie('user_id'),
            'dh'            => $entry['field_1'],
            'xm'            => $entry['field_2'],
            'shiping_time'  => $entry['field_3'],
            'dao_time'      => $entry['field_4'],
            'hour_time'     => $entry['field_5']['hour'] . ':' . $entry['field_5']['minute'],
            'shiping_type'  => $entry['field_6'],
            'goods_type'    => $entry['field_7'],
            'good_num'      => $entry['field_8'],
            'yuanyin_other' => $entry['field_9_other'],
            'dt'            => date("Y-m-d H:i:s", time()),
        ];
        if (empty($data['dh'])) {
            return json(['code' => 0, 'msg' => '请填写电话']);
        }
        if (empty($data['xm'])) {
            return json(['code' => 0, 'msg' => '请填姓名']);
        }
        if (empty($data['goods_type'])) {
            return json(['code' => 0, 'msg' => '请填品种']);
        }
        if (empty($data['good_num'])) {
            return json(['code' => 0, 'msg' => '请填数量']);
        }
        if (is_array($entry['field_9'])) {
            foreach ($entry['field_9'] as $key => $value) {
                $data['yuanyin'] .= $value . '/';
            }
        } else {
            $data['yuanyin'] = $entry['field_9'];
        }
        Db::startTrans();
        $cid   = Db::name('user_complaint')->insertGetId($data);
        $files = request()->file('img1');
        if (empty($files)) {
            return json(['code' => 0, 'msg' => '提交失败,请选择图片']);
        }
        $files2 = request()->file('img2');
        if (empty($files2)) {
            return json(['code' => 0, 'msg' => '提交失败,请选择图片']);
        }
        foreach ($files as $file) {
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size' => 5767168])->move(ROOT_PATH . 'public' . DS . 'backload');
            if ($info) {
                $type = 1;
                $url  = '/public' . DS . 'backload/' . $info->getSaveName();
                Db::name('user_complaint_img')->insert(['cid' => $cid, 'path' => $url, 'type' => $type]);
            } else {
                Db::rollback();
                return json(['code' => 0, 'msg' => '文件太大提交失败']);
            }
        }
        foreach ($files2 as $file) {
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size' => 5767168])->move(ROOT_PATH . 'public' . DS . 'backload');
            if ($info) {
                $type = 2;
                $url  = '/public' . DS . 'backload/' . $info->getSaveName();
                Db::name('user_complaint_img')->insert(['cid' => $cid, 'path' => $url, 'type' => $type]);
            } else {
                Db::rollback();
                return json(['code' => 0, 'msg' => '文件太大提交失败']);
            }
        }
        Db::commit();
        return json(['code' => 1, 'msg' => '提交成功!']);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值