Form submit button in table without function - laravel

Can anyone explains me, why the submit buttons no and yes in the following code not trigger?
Without the table, it works.
<table class="table">
<thead>
<tr>
<th scope="col">Group</th>
<th scope="col">Yes No</th>
</tr>
</thead>
#foreach($invitations as $invitation)
{!! Form::open(array('route'=>'store.groupentry')) !!}
<tbody>
<tr>
<td>
Do you want to enter group {{$invitation->group_name}}?
</td>
<td>
<input type="hidden" name="idgroup" value="{{ $invitation->idgroup }} "/>
<input type="hidden" name="groupname" value="{{ $invitation->group_name }} "/>
<button type="submit" class="btn btn-default" name = "submitbutton" value = "save">Yes</button>
<button type="submit" class="btn btn-default" name = "submitbutton" value = "nosave">No</button>
{{ csrf_field() }}
</td>
</tr>
</tbody>
{!! Form::close() !!}
#endforeach
</table>

Maybe not valid html generation. You generate many forms wrap many tbody in one table. I think you need create one form, and before submit set hidden values with js

Related

how to update an array in laravel

i have an array of data in a form that i want to update.when i update only the last column updated while the inputs that i updated do change.for example, we have a column size, price, and stock. the size include small,medium, and large and their respective prices and stock number. when i update the price of small size,it doesnt update but rather it updates the medium size.same for the large size also.i havent understood why only a specfic column is updating yet i have added a foreach to loop all the columns when updating.here is my update function.
public function editattributes(Request $request,$id)
{
$merchadisedata=Merchadise::select('id','merch_name','merch_code','merch_image')->find($id);
if ($request->isMethod('post')){
$data=$request->all();
foreach($data['attrid'] as $key=>$attr){
if(!empty($attr)){
Productattribute::where(['id'=>$data['attrid'][$key]])
->update([
'productattr_price'=>$data['productattr_price'][$key],
'productattr_stock'=>$data['productattr_stock'][$key],
]);
}
$message='Product attributes have been updated successfully';
Session::flash('success_message',$message);
return redirect()->back();
}
}
return view('backend.merchadise.editproductattributes')->with(compact('merchadisedata'));
}
on dd($data);die(); i get the inputs i have filled
here is the form in the blade file that am submitting
<form method="post" action="{{ url('admin/edit-attributes/'.$merchadisedata->id) }}">
{{csrf_field()}}
<table id="products" class="table table-striped table-bordered nowrap" style="width:100%;">
<thead>
<tr>
<th>Attribute Size</th>
<th>Attribute Stock</th>
<th>Attribute Price</th>
<th>Attribute Sku</th>
</tr>
</thead>
<tbody>
#foreach ( $merchadisedata->merchadiseattributes as $attribute)
<input type="text" name="attrid[]" value="{{ $attribute->id }}" style="display: none"/>
<tr>
<td>{{ $attribute->productattr_size}}</td>
<td>
<input type="number" name="productattr_stock[]" value="{{ $attribute->productattr_stock }}" required=""/>
</td>
<td>
<input type="number" name="productattr_price[]" value="{{ $attribute->productattr_price }}" required=""/>
</td>
<td>{{ $attribute->productattr_sku }}</td>
</tr>
#endforeach
</tbody>
</table>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sm btn-block">Submit</button>
</div>
</form>
where might i be going wrong in my code

passing a query result received from a controler in blade, back to another controller in laravel 8

I have a view that received a query result $necps from a controller, and I want to send to another controler with a form like below.
I am using a foreach to create the hidden inputs, but in the controller I get just the last record.
First question is : can I do that in a more elegant way? Sendind the entire query result at once?
Second : If not, how can I send all the array using foreach?
No Js please. Thanks a lot.
#if (isset($necps))
<div style="float:left; margin-left:20px;" >
<form style="display:inline;" action="{{route('mostra_varios_parts')}}" method="post">
#csrf
<button type="submit" class="btn btn-sm btn-mapa bi-globe texto_p"> Mapa</button>
#foreach($necps as $necp)
<input value="{{$necp->id_part}}" name="parts[id]" type="hidden">
<input value="{{$necp->latitude}}" name="parts[latitude]" type="hidden">
<input value="{{$necp->longitude}}" name="parts[longitude]" type="hidden">
<input value="{{$necp->nome_part}}" name="parts[nome_part]" type="hidden">
<input value="{{$necp->endereco}}" name="parts[endereco]" type="hidden">
#endforeach
</form>
</div>
<br>
<br>
<table class="table table-sm tabela-necessidade">
<thead>
<tr>
<th scope="col" class="texto_p">Nome</th>
<th scope="col" class="texto_p">Necessidade</th>
<th scope="col" class="texto_p">Categoria</th>
<th scope="col" class="texto_p">Data</th>
<th scope="col" class="texto_p">Quant</th>
<th scope="col" class="texto_p">Unidade</th>
<th scope="col" class="texto_p">Local</th>
<th scope="col" class="texto_p">Observações</th>
<!--<th scope="col" class="texto_p">Distância/Kms</th>-->
<th class="texto_p" colspan="1">Ações</th>
</tr>
</thead>
<tbody>
#if (count($necps)>0)
#foreach($necps as $necp)
<div>
<tr>
<td class="texto_p">{{$necp->nome_part}}</td>
<td class="texto_p">{{$necp->desc_nec}}</td>
<td class="texto_p">{{$necp->desc_cat}}</td>
<td class="texto_p">{{$necp->data}}</td>
<td class="texto_p">{{$necp->quant}}</td>
<td class="texto_p">{{$necp->desc_unid}}</td>
<td class="texto_p">{{$necp->endereco}} - {{$necp->cidade}}</td>
<td class="texto_p">{{$necp->obs}}</td>
<!--<td class="texto_p">{{$necp->distancia}}</td>-->
<td>
<form action="" method="post">
#csrf
<button type="submit" class="btn btn-sm btn-conectar bi-arrow-repeat texto_p"> Conectar</button>
<input value="{{$part->id}}" name="id_part_t" type="hidden">
<input value="{{$necp->id_nec_part}}" name="id_nec_part_t" type="hidden">
</form>
</td>
</tr>
</div>
#endforeach
#else
<td><td>Nenhum registro encontrado</td></td>
#endif
</tbody>
</table>
#endif
The name attribute for your hidden inputs need to be unique. you can post the fields as an array like so:
#foreach($necps as $necp)
<input value="{{$necp->id_part}}" name="parts[{{ $loop->index }}][id]" type="hidden">
<input value="{{$necp->latitude}}" name="parts[{{ $loop->index }}][latitude]" type="hidden">
<input value="{{$necp->longitude}}" name="parts[{{ $loop->index }}][longitude]" type="hidden">
<input value="{{$necp->nome_part}}" name="parts[{{ $loop->index }}][nome_part]" type="hidden">
<input value="{{$necp->endereco}}" name="parts[{{ $loop->index }}][endereco]" type="hidden">
#endforeach
with $loop->index as array key you post 1 array for every $necp

How do i sent dynamic values from form (post method) back to controller

I am quite new to laravel,
Basically, I have this Form where I am showing values Dynamically, and those values i need to pass into controller again for Delete, but not understanding how to do it.
my blade is as bellow
<div class="col-md-12">
#if(isset($rtn_user_id))
#csrf
<table>
<tr>
<td>Customer Name</td> <td>Vechile Name</td> <td>Imei</td><td>Actions</td>
</tr>
<tr>
<td> {{$rtn_user_id}}</td> <td></td> <td> {{$rtn_device_name}}</td> <td>{{$rtn_imei}}</td> <td><button type="button">Delete</button></td>
</tr>
</table>
#endif
</div>
</form>
my operation.delete_imei is like this
so I want to pass the values of $rtn_imei to the controller
public function delete_imei(Request $request)
{
$post_imei = $request->imei;
dd($pos_imei)
}
web.php
Route::post('deleteimei', 'Operation#delete_imei')->name('operation.delete_imei');
Can you please help me with this
You need to issue an HTTP post to /deleteimei, which is really an HTML/JS question, depending on how you choose to do it.
Assuming you're not using a JS library which makes life easier, this is how you'll want to do it (change the selectors etc. to suit):
document.getElementsByTagName('button').click = function() {
xhttp.open('POST', '/deleteimei', true);
xhttp.send();
location.reload();
}
In Blade File
#csrf
#if(isset($rtn_user_id))
<tr>
<td>Customer Name</td> <td>Vechile Name</td> <td>Imei</td><td>Actions</td>
</tr>
<tr>
<td> {{$rtn_user_id}}</td> <td></td> <td> {{$rtn_device_name}}</td> <td>{{$rtn_imei}}</td> <td><button formaction="{{route('operation.delete_imei',$rtn_imei)}}" type="submit" class="btn btn-danger btn-sm"><i class="fa fa-trash-o" aria-hidden="true"></i></button></td>
</tr>
</table>
#endif
</div>
</form>
web.php
Route::delete('deleteimei/{id}', 'Operation#delete_imei')->name('operation.delete_imei');
-In Operation Controller
public function delete_imei($id) {
$post_imei = $id;
dd($pos_imei)
$data=DB::delete('delete from delete_imei where id=?',[$id]);
return redirect(route('index'));
}
i think you should edit your blade like this and this will send your form's data to your controller and you will be suitable for get your request's datas.
<form role="form" method="POST" action="{{ route('operation.delete_imei') }}">
#csrf
<div class="col-md-12">
#if(isset($rtn_user_id))
<table>
<tr>
<td>Customer Name</td> <td>Vechile Name</td> <td>Imei</td><td>Actions</td>
</tr>
<tr>
<td> {{$rtn_user_id}}</td> <td></td> <td> {{$rtn_device_name}}</td> <td>{{$rtn_imei}}</td> <td><button type="button">Delete</button></td>
</tr>
</table>
#endif
</div>
</form>
if you want to pass some data from blade to controller just put some hidden input in your form and set the value of that with your data you want to pass:
<input id="invisible_id" name="invisible" type="hidden" value="{{$data}}">

Form is empty when submitted using Thymeleaf

I want to fill a list of applications for a university in the frontend. Each entry is supposed to hold two buttons: one for accepting the application and one for rejecting it. I created one form for each submit-button each.
<div class="container">
<div class="row">
<div class="col-12">
<table class="table table-hover">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Firstname</th>
<th scope="col">Lastname</th>
<th scope="col">Grade</th>
<th scope="col">NC</th>
<th scope="col">Course</th>
<th scope="col">Certificate</th>
<th scope="col">Recommendation</th>
<th scope="col">Decision</th>
</tr>
</thead>
<tbody>
<tr th:each="applicationOpen, rowStat: ${lstOpApplications}">
<th th:text="${rowStat.count}">1</th>
<td th:text="${applicationOpen.firstname}">firstname</td>
<td th:text="${applicationOpen.lastname}">lastname</td>
<td th:text="${applicationOpen.highschool_grade}">grade</td>
<td th:text="${applicationOpen.nc}">nc</td>
<td th:text="${applicationOpen.name}">coursename</td>
<td th:text="${applicationOpen.highschool_certificate}">certificate</td>
<td th:text="${applicationOpen.document}">recommendation</td>
<td>
<form action="#" th:action="#{/Bewerberubersicht}" th:object="${decisionForm}" method="post">
<input type="hidden" name="application_id" th:field="*{application_id}" value=${applicationOpen.id}"/>
<input type="hidden" name="decision" th:field="*{decision}" value=1/>
<button type="submit" class="btn btn-success"><i class="fas fa-edit"></i>Accept</button>
</form>
<form action="#" th:action="#{/Bewerberubersicht}" th:object="${decisionForm}" method="post">
<input type="hidden" name="application_id" th:field="*{application_id}" value=${applicationOpen.id}/>
<input type="hidden" name="decision" th:field="*{decision}" value=2/>
<button type="submit" class="btn btn-danger"><i class="fas fa-edit"></i>Reject</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
When I look in the Browser's Dev-Tools I can see that the content of the POST-request is
application_id: ""
decision: ""
When I replace value=${applicationOpen.id} with e.g. value=5 it is still empty. Hence, it should not be a problem with applicantOpen. Also, the list in the frontend is being filled just fine, so all of that should work. I first thought is is a problem with the DecisionForm class, but it seems my subsequent problems are caused by the issue described here.
The th:field attribute overrides content of name and value attributes. The decisionForm object seems to have empty fields so all forms have empty values.
Basically using th:object with th:field is convenient when you need to have your form prepopulated with values. It establishes initial state of your form, not the target. Using them makes sense for single form, not for multiple forms in loop with varying values.
In your case: please remove both th:object and th:field attributes. Instead use value="1" or value="2" for decision input, and th:value="${applicationOpen.id}" for application_id input.

Uploading image can't saved at directory and having error "htmlspecialchars() expects parameter 1 to be string, array given "

i have a view input like that :
<form class="form-group" action="/user6" method="post" enctype="multipart/form-data">
<table class="table table-striped">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th style="width: 60px">Tidak Baik</th>
<th style="width: 60px">Baik</th>
</tr>
<div class="form-group">
<tr>
<td>1.</td>
<td><input class="form-control" style="border:none" type="text" name="question1" value="Kondisi, kebersihan, pelumasan bearing" readonly></td>
{{-- <td><input class="form-control" type="text" placeholder=".input-lg"></td> --}}
<td>
<label><input type="radio" name="answer1" value="tidak baik" checked></label>
</td>
<td>
<label><input type="radio" name="answer1" value="baik"></label>
</td>
</tr>
<tr>
<td></td>
<td> <input class="form-control" style="border:none" type="file" name="image" value="" readonly> </td>
</tr>
</div>
<tr>
<td></td>
<td> <div class="form-group">
<label>Catatan</label>
<textarea class="form-control" name="catatan" rows="3" placeholder="Enter ..." required></textarea>
</div>
</tr>
<input type="hidden" name="alat_id" value="7">
<input type="hidden" name="status" value="3 Bulanan">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</tbody>
</table>
<div class="box-footer">
<button type="submit" class="btn btn-primary" name="name" value="post">Submit</button>
</div>
</form>
and to save this i have controller :
public function store6(Request $request)
{
if($request->hasfile('image'))
{
foreach($request->file('image') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan']));
$user->catatan = $request->catatan;
$user->status = $request->status;
$user->save();
//dd($user);
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
before i add "enctype="multipart/form-data" , my view dont have error BUT cant display and saved image at directory . i want saved image to directory but cant saved .
i create a folder named 'images' at public . after i add enctype="multipart/form-data" . this view having error "htmlspecialchars() expects parameter 1 to be string, array given "
its my view after input :
<table class="table table-condensed">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th>Hasil</th>
{{-- <th style="width: 40px">Label</th> --}}
</tr>
<tr>
<td>1.</td>
<td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['answer1'] }}
</td>
</tr>
<tr>
<td>2.</td>
<td>{{ $pemeliharaan->pertanyaan['image'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['image'] }}
</td>
</tr>
<td><img src="{{ url('images/'.$pemeliharaan->pertanyaan['image'])}}"></td>
</tbody></table>
Something in your table is array, it's not a string so when you put it inside {{}}, it will show error.
You should check
$pemeliharaan->pertanyaan['question1']
$pemeliharaan->pertanyaan['answer1']
$pemeliharaan->pertanyaan['image']
by using dd() function.
Deal with one problem at a time. At present you are storing the image but not saving the path to the file, which you saved in $data. You are not using $data anywhere.
Then you have a problem rendering the new model in the view. This is a totally different issue and is caused by having non-string data passed to the htmlspecialchars function which is what {{ }} does.

Resources