Submit multiple forms created by foreach loop with one button - html

In my blade template, I use a foreach to create one form per iteration. Currently, each form has a submit button, but I want to use one submit button to submit all the form, because now I cannot submit all forms at the same time. How can I fix this?
#if(count($task_criterias) > 0)
#foreach($task_criterias as $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
<div class="form-group" hidden >
{{ Form::label('criteria_id', 'criteria_id') }}
{{ Form::text('criteria_id', $task_criteria->id, ['class'=>'form-control']) }}
</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark', '',['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
{!! Form::close() !!}
</td>
#endforeach
<input type="button" class="btn btn-info " value="Submit" onclick="submitForms()" />
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'/criteria_marks/post',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
#endif

There can be only one post submit per request. One of the easiest solution is to perfom multiple requests using ajax:
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'* route to CriteriaMarksController#store *',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
Or you can make one big form:
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
#foreach($task_criterias as $key => $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark['.$task_criteria->id.']', '',
['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
</td>
#endforeach
<input type="submit" class="btn btn-info " value="Submit" />
{!! Form::close() !!}
This will form associative array selfmark in post with id => value pairs

Not sure how to do it with the Form facade.
What you have now is:
<input name="criteria_id" value="{{ $task_criteria->id }}" />
<input name="selfmark" value="{{ $task_criteria->id }}" />
But what you need to have is:
<input name="criteria_id[]" value="{{ $task_criteria->id }}" />
<input name="selfmark[]" />
Note the [] in the input names.

Related

Image doesn't load after encoding blob from database(php,mysql,laravel)

I'm trying to create a picture for every contact.
this is the store() method in my controller:
public function store()
{
$attributes = request()->validate([
'user_id' => ['required'],
'avatar' => ['required',],
'ime' => ['required', 'min:3'],
'prezime' => ['required', 'min:3'],
'broj' => ['required', 'min:3'],
]);
Kontakt::create($attributes);
return redirect('/imenikk');
}
this is the create.blade.php:
<form method="POST" action="/imenikk">
{{ csrf_field() }}
<div class="container text-center">
<label for="ime">Ime</label>
<input type="text" class="form-control text-center" name="ime"
placeholder="Ime">
<label for="prezime">Prezime</label>
<input type="text" class="form-control text-center" name="prezime"
placeholder="Prezime">
<label for="broj">Broj</label>
<input type="text" class="form-control text-center" name="broj"
placeholder="Broj">
<br>
<input type="file" name="avatar" accept="image/*">
<br>
<input type="hidden" name="user_id" value='{{$user_id}}'>
<button type="submit" class="btn btn-primary">Dodaj
</div>
</form>
And this is on show.blade.php:
<img src="data:image/jpg;base64,{{ chunk_split(base64_encode($imenikk->avatar)) }}" height="500" width="500">
<img src ="data:image/jpeg;base64,{{base64_encode($imenikk->avatar)}}" height="200" width="200">
Neither of these 2 are working.. I get this but the image doesn't load:
<img src="data:image/jpg;base64,SU1HXzYxODQuSlBH" height="500" width="500">
This is how my db looks when I create a contact with picture:
Also I have managed to decode this
img src="data:image/jpg;base64,SU1HXzYxODQuSlBH
and I get the image name when I decode it yet the page still doesn't display the image.
It seems your base64 code is wrong.
Try to get the real base64 code from image file, and store in database.
public function store()
{
$attributes = request()->validate([
'user_id' => ['required'],
'avatar' => ['required',],
'ime' => ['required', 'min:3'],
'prezime' => ['required', 'min:3'],
'broj' => ['required', 'min:3'],
]);
$file = request()->file('avatar');
$imagedata = file_get_contents($file->getRealPath());;
$base64 = base64_encode($imagedata);
$attributes['avatar'] = $base64;
Kontakt::create($attributes);
return redirect('/imenikk');
}
then store it in your field,
and display it like this:
<img src={{ "data:image/jpg;base64,".$imenikk->avatar }} height="500" width="500">

Laravel 6 - How to create a table with dynamic rows using LaravelCollective?

I want to make a dynamic table using LaravelCollective, where you can add/remove rows.
I already tried to follow a few tutorials, but I keep failing. I don't know what I'm doing wrong.
Html Code for the table/form
{{ Form::open(['action' => 'TransactionsINController#store', 'method' => 'POST']) }}
<section>
<div class="panel panel-header">
<div class="form-group">
{{ Form::label('supplier_name', 'Supplier Name') }}
{{ Form::select('supplier_name', $supplierList->pluck('name', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick one supplier...']) }}
</div>
<div class="form-group">
{{ Form::label('transaction_in_date', 'Transcation Date') }} <br>
{{ Form::date('transaction_in_date', \Carbon\Carbon::now()->format('d M Y')) }}
</div>
</div>
<div class="panel panel-footer">
<table class="table table-hover table-bordered" id="">
<thead align="center">
<tr>
<th>Device Type</th>
<th>Spec</th>
<th>Price</th>
<th><a href="#" class="btn btn-success addRow">
<i class="fa fa-plus"></i>
</a>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="form-group">
{{ Form::select('device_type_name[]', $deviceTypeList->pluck('name_tipe_device', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick Device Type...', 'name' => 'device_type_name[]']) }}
</div>
</td>
<td>
<div class="form-group">
{{ Form::textarea('device_spec[]', '',
['class' => 'form-control', 'placeholder' => 'Device Spec', 'rows' => 5, 'cols' => 45, 'name' => 'device_spec[]']) }}
</div>
</td>
<td>
<div class="form-group">
{{ Form::number('device_price[]', 'value', ['name' => 'device_price[]']) }}
</div>
</td>
<td>
<a href="#" class="btn btn-danger remove">
<i class="fa fa-times"></i>
</a>
</td>
</tr>
</tbody>
</table>
</div>
</section>
{{ Form::button('<i class="far fa-save"></i> Submit', ['type' => 'submit', 'class' => 'btn btn-info'] ) }}
{{ Form::close() }}
The Script i use to add/remove Rows
<script type="text/javascript">
$('.addRow').on('click', function(){ \\Uncaught ReferenceError: happen in this line.
addRow();
});
function addRow(){
var tr = '<tr>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::select('device_type_name[]', $deviceTypeList->pluck('nama_tipe_device', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick Device Type...', 'name' => 'device_type_name[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::textarea('device_spec[]', '',
['class' => 'form-control', 'placeholder' => 'Device Spec', 'rows' => 5, 'cols' => 45, 'name' => 'device_spec[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::number('device_price[]', 'value', ['name' => 'device_price[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<a href="#" class="btn btn-danger remove">'+
'<i class="fa fa-times"></i>'+
'</a>'+
'</td>'+
'</tr>';
$('tbody').append(tr);
}
</script>
When I press the + button to add more rows it doesn't do anything and when I inspected the code in the browser, I found this error:
Uncaught ReferenceError: $ is not defined
$ refers to jQuery, have you added it to your project and is it available in this scope?
Note: it needs to be present before your script
jQuery may not have loaded when that script runs, you can try wrapping your script with $(document).ready({ }) so it runs it when jQuery has loaded

Pass data from class select to get route

how can I pass data from this form to my route?
<div class="panel-body">
{!! Form::open(array('route'=>'show.exclusion')) !!}
<div class="form-group">
{{Form::label('choosegroup', 'Wähle eine Gruppe')}}
<select class="form-control m-bot15" name="idgroup">
#foreach($groups as $group)
<option value="{{ $group->id }}">{{ $group->name }}</option>
#endforeach
</select>
{{ csrf_field() }}
</div>
<div>
{{Form::submit('Search',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
</div>
This is my route...
Route::get('exclusion/show/{id}', 'ExclusionController#show')->name('show.exclusion');
First I put the $group->id parameter into the array route part. But I cannot use them at this point of my code.
Any Ideas?
EDIT:
public function show($id)
{
$member = Nerd::find($id);
return view('groups.test')->with('member', $member);
}
You need to pass the whole Request to the function. Than it is possible to get the Values of your Formdata.
public function update(Request $request, $id)
{
$idgroup = $request->input('idgroup');
}
For more details have a look here:
https://laravel.com/docs/5.6/requests
You can use request() method to fetch form data without any class dependencies.
For example:
request()->id;

Keep the option selected in search form - Laravel

I have a search form that searches for responsible, date and device name. When someone searches for a resposible with a date, I want that search data to be maintained in the result
The entire search form
{{ Form::open(['route' => 'reviews.index', 'method' => 'GET', 'class' => 'form-inline pull-right']) }}
#csrf
{{ Form::text('device_name', null, ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
<select name="name">
<option></option>
<option>OK</option>
<option>NOK</option>
</select>
{{ Form::date('created_at', null, ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
<select name="responsable">
<option ></option>
#foreach($users as $user)
<option>{!! $user->name !!}</option>
#endforeach
</select>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Buscar</button>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Reset</button>
{{ Form::close() }}
And the index method in Controller
public function index(Request $request)
{
$responsable = $request->get('responsable');
$created_at = $request->get('created_at');
$device_name = $request->get('device_name');
$name = $request->get('name');
$devices = Device::all();
$reviews = Review::orderBy('id', 'DESC')
->responsable($responsable)
->created_at($created_at)
->device_name($device_name)
->name($name)
->paginate(30);
$users = User::all();
return view('reviews.index', compact('devices', 'reviews', 'users'));
}
Thanks
You may use old() function for re-populate the form with the last submitted data.
See https://laravel.com/docs/5.6/requests#old-input
So, in your blade code it would be something like this:
{{ Form::text('device_name', old('device_name'), ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
//...
{{ Form::date('created_at', old('created_at'), ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
For select, it's different, basically you compare each rendered option in the loop and if it's the one submitted you mark it as selected with html.
<select name="responsable">
<option></option>
#foreach($users as $user)
<option value="{{ $user->name }}" {{ old('responsable') == $user->name ? 'selected' : '' }}>{!! $user->name !!}</option>
#endforeach
</select>
Also, consider that old() also accepts a second parameter for default value in case you don't want it to be just null.

Symfony3 apply class to each div of generated form

I want to apply a css class 'pure-control-group' to all div of a generated form.
<form name="form" method="post" class="pure-form pure-form-aligned">
<div>
<label for="form_Title" class="required">Titre</label>
<select id="form_Title" name="form[Title]" class="pure-control-group">
<option value="Modification" >Modification</option>
<option value="Construction" >Construction</option>
<option value="Autre" >Autre</option>
</select>
</div>
<div>
<label for="form_ContactWay" class="required">Moyen de Contact</label>
<select id="form_ContactWay" name="form[ContactWay]" class="pure-control-group">
<option value="Telephone" >Téléphone</option>
<option value="Email" >Email</option>
<option value="Direct" >Direct</option>
<option value="Autre" >Autre</option>
</select>
</div>
<div>
<label for="form_Log" class="required">Journal</label>
<textarea id="form_Log" name="form[Log]" required="required" class="pure-control-group"></textarea>
</div>
<div>
<button type="submit" id="form_Enregistrer" name="form[Enregistrer]">Enregistrer</button>
</div>
<input type="hidden" id="form__token" name="form[_token]" value="c19WunU5AgDgc954I3DRJXLqEhQwpOyDCBZEpF7akJs" />
</form>
I tried :
$this->logForm = $this->createFormBuilder($log, array('allow_extra_fields' => true))
->add('Title', ChoiceType::class, array(
'label' => 'Titre',
'choices' => array(
'Modification' => 'Modification',
'Construction' => 'Construction',
'Autre' => 'Autre'),
'attr'=> array('class'=>'pure-control-group')))
->add('ContactWay', ChoiceType::class, array(
'label' => 'Moyen de Contact',
'choices' => array(
'Téléphone' => 'Telephone',
'Email' => 'Email',
'Direct' => 'Direct',
'Autre' => 'Autre'),
'attr'=> array('class'=>'pure-control-group')))
->add('Log', TextareaType::class, array(
'label'=> 'Journal',
'attr'=> array('class'=>'pure-control-group')))
->add('Enregistrer', SubmitType::class)
->getForm();
The problem is that the class is add to the input. label_attr do the job but for the label.
How can I do for the div?
Notice that I would appreciate not to render each field by hand.
You can modify the template for the form row. Do this in the template that the form is rendered in:
{% form_theme form _self %}
{%- block form_row -%}
<div class="pure-control-group">
{{- form_label(form) -}}
{{- form_errors(form) -}}
{{- form_widget(form) -}}
</div>
{%- endblock form_row -%}