douhuan6065 2018-06-02 19:13
浏览 55
已采纳

如何使用ajax和laravel 5.6保存多个输入和文件

I have about twenty text-box, checkbox and select as well as four file type inputs. I would like to save the input to a database using Laravel and AJAX.

Actually, I can save all inputs except file type (images).

Table: META | VALUE

My route: $this::resource('/settings','SettingController')->only(['index', 'store']);

Controller:

$setting = $request->all();
foreach ($setting as $meta => $value) {
    $SF = new Setting();
    $SF->meta = $meta;
    $SF->value = $value;
    echo  $SF->save();
}

HTML:

<form>
 <input type="text" name="site_title"  id="site-title">
 <input type="checkbox" name="register"  id="register">
  ....
 <input type="file" name="logo"  id="logo">
 <input type="file" name="favicon"  id="favicon">
 <input type="file" name="user_profile"  id="user_profile">
 <input type="file" name="Personal"  id="Personal">

AJAX code:

 $('#setting-forms').on('submit', function () {
    event.preventDefault();
    let _token =  $('input[name=_token]').val();  //Get Token
    let FormInputs = $("#setting-forms").serialize(); 

    $.ajax({
        method: 'POST',
        url: '/settings',
        data: FormInputs ,

        success: function (data) {
          //Some Success MSG

        },
        error: function () {
             //Some Error MSG
        },
    });

Now, this code saves all inputs except file types and my result is something like this :

     META     |     VALUE
--------------+--------------
   site_title |   Laravel
    register  |     On
      ....    |     ...

But I want save all image files too, so I changed my AJAX code to the following:

 let formData = new FormData();
 let logo = $("#logo")[0].files[0];
 formData.append('logo', logo);
 formData.append('FormInputs', FormInputs);
 $.ajax({
        method: 'POST',
        url: '/settings',
        data: formData,
        contentType: false,
        processData: false,
        headers: {
            'X-CSRF-TOKEN': _token
        },

Now I can save logos but my text is all saved on a single row. I get this:

     META     |                Value
--------------+-----------------------------------
     Logo     |               MyLOGO
   FormInputs |  site_title=titlea&register=on&...

While I would like something like that:

     META     |     VALUE
--------------+--------------
    Logo      |    MyLOGO
  site_title  |    title
   register   |     On

How can I fix this?

  • 写回答

1条回答 默认 最新

  • douchuoliu4422 2018-06-06 13:51
    关注

    Well,After 3 day finally get my answer. So, I put my code here if anybody needed or have my problem.

    Above code is work just need some changes and here changes.

    //let FormInputs = $("#setting-forms").serialize();  
      let FormInputs = $("#setting-forms").serializeArray();
    
     // formData.append('FormInputs', FormInputs);
     jQuery.each(FormInputs, function (i, field) {
            formData.append(field.name, field.value);
     });
    

    Well, Now work fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试