Laravel foreach loop not outputting to form - php

Here is a form from my Laravel 5 project:
<form class="form-group" action="/counselors/{{ $counselor->id }}/badges/add" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($badges as $badge)
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" style="float: left" value="{{ $badge->id }}"> {{ $badge->name }}
</span>
</div>
#endforeach
<br>
<input type="submit" class="form-control btn btn-primary" name="submit" value="Submit"><hr>
<input type="button" class="form-control btn btn-danger" name="cancel" value="Cancel" onClick="location='/counselors'">
</form>
The $badges is a collection of badges from a database. They all iterate and display properly, but when i look at the request data with
$request->all()
it returns:
{"_token":"5E4csIJU4YVVQZSoPG1EmpfHfjNjYcRwuOoreCcE","submit":"Submit"}
It doesn't include the checkbox data from the loop. Is this incorrect? What's the best way to do this? There are 160 items in the $badges collection.
I'm pretty new so please go easy.

put name attribute to the input checkbox and giv it a value
<input type="checkbox" name="check-{{ $badge->id }}" style="float: left" value="{{ $badge->id }}"> {{ $badge->name }}
Note :
here i use the name "check-" and the id of the badge to get defferent name for each checkbox

Related

Save editable text on input and save into db

I have an input for a bitcoin wallet address, but I need 13 more, and this code is very long and would be huge, besides I've tried this way and it didn't work. How you would do this simply?
This alone works very well, the user has the wallet you want and clicks update and is saved in the database.
My html:
<form role="form" method="post" action="">
{{ csrf_field() }}
{{ method_field('POST') }}
<span><i>Wallet:</i></span>
<small id="emailHelp" class="form-text text-muted text-center">
<div class="input-group">
<input type="text" class="form-control" placeholder="Bitcoin wallet code" name="bitzpayer_id2" value="{{old('bitzpayer_id2') ? old('bitzpayer_id2') : Auth::user()->bitzpayer_id2}}"/>
<span class="input-group-btn">
<button type="submit" class="btn btn-sm btn-info">Update</button>
</span>
</div>
</small>
</form>
You can make 13 different form with a hidden text field called id.
<input type="hidden" id="{{ $id }}" />
Then you will have your desired request data.

How to pass correct data from a dynamic button in Laravel 5.8

I have a dynamic buttons where it produced from my database
My code for that in blade file
<div class="input-group col-sm-12">
<!--start of the form-->
<form class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<!--input type hidden department code below -->
#foreach($departments as $department)
<input type="hidden" id="dept_name" name="dept_name" value="{{ $department->dept_name }}">
<input type="hidden" id="called" name="called" value="NO">
<!--buttons -->
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
{{ $department->dept_name }}
</button>
#endforeach
</form>
<!--end-->
</div>
Whenever I click either one, they'all add a data in my call database based on the values per button. My problem is when I click the cashier button, the values that would add would be the assessments.
My code in CallController
public function store(Request $request)
{
$dept_id = Department::select('id')
->where('dept_name', $request->input('dept_name'))
->first();
$let = Department::select('letter')
->where('dept_name', $request->input('dept_name'))
->first();
$number = Department::select('start')
->where('id', $dept_id->id)
->first();
$call = Call::create([
'dept_id' => $dept_id->id,
'letter' => $let->letter,
'number' => $number->start,
'called' => $request->input('called')
]);
Department::where('id', $dept_id->id)
->increment('start');
return redirect()->route('call.index')->with('success' , 'NEW CALL');
}
I also dd each query and found out that the values would get are the values from assessment or the last value from the foreach loop in my blade file. How could I get the value of cashier when I click the cashier button instead of assessment.
I would show my database so that you understand my question
Department Table: id, dept_name, letter, start(int, it'll increment after producing a call)
Counter: id, counter_num, dept_id
Call Table: id, dept_id, letter, number, counter_id, called
When you click either button, they will submit their parent form. Because both are under the same form, the data from the first button will be submitted. You will have to make a separate form for each button in order to have them submit their own data.
<div class="input-group col-sm-12">
#foreach($departments as $department)
<form class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<input type="hidden" id="dept_name" name="dept_name" value="{{ $department->dept_name }}">
<input type="hidden" id="called" name="called" value="NO">
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
{{ $department->dept_name }}
</button>
</form>
#endforeach
</div>
You're creating multiple submit buttons in the same form.
Add the tag inside the loop so you have a unique form per case.
When there's multiple submit buttons inside the same form, the loop will finish and only the last results will be mapped to the buttons, thus is will submit the last information in your loop.
#foreach($departments as $department)
<form class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<input type="hidden" id="dept_name" name="dept_name" value="{{ $department->dept_name }}">
<input type="hidden" id="called" name="called" value="NO">
<!--buttons -->
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
{{ $department->dept_name }}
</button>
</form>
#endforeach

Form clears parameter from given action url - PHP Laravel

<form role="search" method="get" action="{{ route('people.search', array_replace(Request::all(), [" type" => "customers"])) }}">
<div class="input-group">
<input type="text" name="search" class="form-control" placeholder="Search...">
<div class="input-group-btn">
<button class="btn btn-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
The following form action link looks like this:
http://127.0.0.1:8080/people/search/customers?countries=Germany&statuses=3&page=1
But when I submit the search form, it redirects to:
http://127.0.0.1:8080/people/search/customers?search=test
Instead of including the parameters that were previously set.
Is there a way to prevent that?
What I did to include the previous parameter is just by adding them as hidden fields:
#foreach(request()->query() as $key => $value)
#if($key == "page") // Don't want the page parameter to be in there
#continue
#endif
<input type="hidden" name="{{$key}}" value="{{$value}}">
#endforeach

My PHP submit button does not work. How to solve it?

I am using Laravel 5 and I have some forms in one page. All is works except the last one. It is nothing happen, no error. I tried to put alert in the button. The alert showed, but the data won't saved.
<tr>
<div>
<form action="{{ url('AddComment') }}" method="POST">
<div>
<td>
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<button id ="af" class="btn btn-round btn-success">Submit</button>
</td>
</div>
</form>
</div>
</tr>
You need to have one submit button to submit a form until you submit it using jquery.
<tr>
<div>
<form action="{{ url('AddComment') }}" method="POST">
<div>
<td>
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="submit" value="Submit"> // This is SUBMIT button.
</td>
</div>
</form>
</div>
</tr>
Or,
If you want to keep the <button> as it is, then you should use jquery/JS function to submit it.
add button type too
<button id ="af" class="btn btn-round btn-success" type="submit">Submit</button>
You are not sending CSRF token in the form. Please use CSRF token in your form.
{ csrf_field() }}
Then you need to use
<input type="submit"/>
for submitting form.If you want to use
<button/>
to sbumit form,then you have to submit your form using jquery. Your form should look like.
<form action="{{ url('AddComment') }}" method="POST">
{ csrf_field() }}
<div>
<td>
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="submit" value="Submit">
</td>
</div>
</form>
Check these points:
1. Add { csrf_field() }} into your form
2. Change button to input type submit (<input type="submit"/>)
3. Check your modal and find out if all fields are fillable.
4. Print your query and check what sql query has been created.
Add CSRF:
<input type="hidden" value="{{csrf_token()}}" name="_token" id="token">
OR
{!! csrf_field() !!}
Good luck
Thank you for all the answer to helping me. I have tried it all but still nothing happen. But I looked at #JYoThI comment which said 'You can't place the form as a child element of table ,tbody, tr .' then I moved the form tags inside the <td> and the it is work !!
<td>
<form action="{{ url('AddComment') }}" method="POST">
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<button id ="afjk" class="btn btn-round btn-success">Submit</button>
</form>
</td>
Check #af on your JavaScript, maybe you put preventDefault() on it. Because if not, every posted answer should have solved your problem by now

Laravel delete item from table list / db

I am trying to delete item from a generated table of items which are from a database table.
My Route:
Route::delete('destroy/{deviceID}', ['as' => 'destroyDevice', 'uses' => 'DeviceController#destroyDevice']);
My Controller method to delete an item:
public function destroyDevice(Request $request, $deviceId = 0)
{
$device = Device::find($deviceId);
if($device)
{
$device->delete();
return redirect()->route('index')->with('success', 'Erfolgreich gelöscht');
}
else
{
return redirect()->route('index')->with('error', 'Fehler');
}
}
And my blade template:
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<td>
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</td>
</form>
If I click on the button nothing happens no error no Response, what am I doing wrong.
If I click on the third delete button the form holds this:
<form action="http://localhost/app/public/device/destroy/3" method="post" name="delete_device"></form>
You can solve this by putting the form inside a td tag in that table.
Like this:
<td> <!-- <--- put these -->
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
</td> <!-- <--- put these -->
I think the form gets ignored somehow due to not being valid, but I am not 100% sure. May people edit this answer ;)
The parameter is case sensitive so it should be deviceID instead of deviceId
public function destroyDevice(Request $request, $deviceID = 0)
Maybe you have some script that prevents the form to submit, some prevent default maybe on button click or on form submit. Check that.

Categories