【问题标题】:How to pass multiple form datas with one submit button如何使用一个提交按钮传递多个表单数据
【发布时间】:2019-02-10 09:57:39
【问题描述】:

我正在尝试使用多语言发布表单。例如,我有 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
            }
        });
    });
}

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

【问题讨论】:

标签: javascript php ajax laravel forms


【解决方案1】:

尝试在 ajax 中传递多个表单数据

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

【讨论】:

  • 是的,这很有用。但我需要使用 foreach (对于每种语言)。我的表单 ID 是 id="form{{$localeCode}}"。我怎么能得到这个问题。你有什么想法吗?
【解决方案2】:

使用for每个语言循环来尝试blow代码示例:

<?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>

【讨论】:

    【解决方案3】:

    在查看您的代码时,您不需要多个表单即可获得所需的内容。您必须将数组作为请求传递给您的控制器。

    【讨论】:

    • 但是我正在为我拥有的每种语言制作表格。例如,我有 10 种语言。我正在使用单选按钮进行更改,并且我有一个提交按钮。在这种情况下我能做什么?我找不到任何解决方案。正如您所说,我需要将请求作为数组发送到控制器。但我没有。
    【解决方案4】:

    这样的事情怎么样?

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2021-08-11
      • 2016-03-29
      • 1970-01-01
      • 2017-05-03
      • 2016-04-06
      相关资源
      最近更新 更多