如何使用一个提交按钮传递多个表单数据

时间:2018-09-05 09:44:50

标签: javascript php ajax laravel forms

我正在尝试发布具有多种语言的表格。例如,我有en,es,ru语言。我需要将en保存到一张桌子上。必须将ES和RU表单保存到翻译表中。但是我无法将所有数据传递给controller @ store函数。如何将所有数据单独传递到控制器?

我正在紧凑地传递语言。像这样:

public function index()
{
    $langs = [];
    foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
    {
        $langs[]= $localeCode;
    }

    return view('backEnd.langview', compact('langs'));
}

在视图中,我具有每种语言的选项卡面板。这是视图:

@foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
@if($loop->first)
    <div class="tab-pane animated fadeIn text-muted active" id="tab{{$localeCode}}" aria-expanded="false">{{$localeCode}}
        <form method="POST" id="form{{$localeCode}}" action="{{route('send_slug')}}">
        {{csrf_field()}}
            <input type="text" name="main_title" placeholder="title here">
            <input type="text" name="main_slug" placeholder="slug here">
        </form>
    </div>
@else
    <div class="tab-pane animated fadeIn text-muted" id="tab{{$localeCode}}" aria-expanded="false">{{$localeCode}}
    <form method="POST" id="form{{$localeCode}}" action="{{route('send_slug')}}">
    {{csrf_field()}}
        <input type="text" name="title[]" placeholder="title here other languages">
        <input type="text" name="slug[]" placeholder="slug here other languages">

    </form>
    </div>
@endif

@endforeach

我尝试过用ajax:

<script>
var langcodes = @json($langs);
var i = 0;
submitForms = function(){
    langcodes.forEach(function (data) {
        i++;
        var formdata = $('#form'+data).serialize();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            }
        });
        jQuery.ajax({
            url: '{{route('send_slug')}}',
            method: 'post',
            data: {
                formdata
            }
        });
    });
}

但是,每次尝试,我都只能通过第一语言或最后一种语言。我需要单独发送所有语言。 我希望我能表达自己。对不起,我的语言。提前致谢。

4 个答案:

答案 0 :(得分:0)

尝试此操作以在ajax中传递多个表单数据

var formdata = $("#form1, #form1").serialize();

答案 1 :(得分:0)

用于每种语言循环尝试打击代码示例:

<?php
$language = array();
foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties){
    $language[] = "#form".$localeCode;
}

$formString = implode(",",$language);

echo $formString;

?>

<script>
var forms = "<?php echo $formString;?>";
var formdata = $(forms).serialize();

</script>

答案 2 :(得分:0)

看到代码时,您不需要多种形式即可获取所需的内容。您必须将数组作为请求传递给控制器​​。

答案 3 :(得分:0)

这样的事情怎么样?

<form method="POST" action="{{route('send_slug')}}">
    {{csrf_field()}}

    @foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
      <div>
          {{$localeCode}}
          <input type="hidden" name="response[i][lang]">

          @if($loop->first)
                <input type="text" name="response[i][title]" placeholder="title here">
                <input type="text" name="response[i][slug]" placeholder="slug here">
          @else
                <input type="text" name="response[i][title]" placeholder="title other language here">
                <input type="text" name="response[i][slug]" placeholder="slug other language here">
          @endif
      </div>
    @endforeach
 <button type="submit">Send</button>
 </form>