I am trying to post forms with multilanguages. For example i have en, es, ru language. I need to save en to the one table. Es and ru forms are must save into the translations table. But i can't pass the all datas to the controller@store function. How can i pass all datas with separatedly to the controller?
I am passing languages with compact. Like that:
public function index()
{
$langs = [];
foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
{
$langs[]= $localeCode;
}
return view('backEnd.langview', compact('langs'));
}
In the view i have tab panel for each language. This is the view:
@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
I was tried that with 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
}
});
});
}
But every try, i just can pass first language, or last language. I need to send all languages with separatedly. I hope i can express myself. Sorry about my language. Thanks in advance.
try this for pass multiple form data in ajax
var formdata = $("#form1, #form1").serialize();
for using for each language loop to try blow code example:
<?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>
At seeing your code, you don't need multiple forms to get what you want. You have to pass an array as request to your controller.
What about something like this ?
<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>