in my blade i have 2 radio inputs
<form class="" action="{{route('admin.album.search')}}" method="post">
<input id="slug" type="radio" name="search" value="slug" >
<input id="id" type="radio" name="search" value="id">
in the same blade file I test to see old value of radio input
{{ old('search')}}
When I select first radio button and submit the form and page reloads I expect that old will be selected radio button value but nothing comes out. What could I be doing wrong?
Try this:
<form class="" action="{{route('admin.album.search')}}" method="post">
{{ csrf_field() }}
<input id="slug"
type="radio"
name="search"
value="slug"
{{ (old('search') == 'slug') ? 'checked': '' }}>
<input id="id"
type="radio"
name="search"
value="id"
{{ (old('search') == 'id') ? 'checked': '' }}>
N.T. If you submit post form request you must be specify csrf_field(). Otherwise it gives you token mismatch exception.
Related
Is there a way to save label title in laravel.
<label for="name">Name</label>
Suppose I want to save "Name" in database.
You can use a hidden field and make its value the same as the label name.
<input name="label" value="Name" type="hidden">
Or you can use something like
<form action="youraction" >
<label for="male">Male</label>
<input type="radio" name="gender" id="male" value="male"><br>
<input type="submit" value="Submit">
</form>
W3Schools - documentation
It will also depend on your use-case may be more information will be helpful.
according to your structure, the label name comes from db, so when you are rendering the blade, you can make a hidden input with value of label name like below:
<form action="{{ route('sampleRoute') }}" >
<label for="{{ $DBVARIABLE }}">{{ $DBVARIABLE }}</label>
<input type="text" name="{{ $DBVARIABLE }}" id="{{ $DBVARIABLE }}" value="{{ $DBVARIABLE }}" style="visibility: hidden;">
<input type="submit" value="Submit">
</form>
I am having the following form, which I would like to submit to my backend.
<form id="revisionFilter" action="{{ route('revision.filter') }}" method='POST'>
<input class="form-check-input" type="checkbox" value="" id="checkbox1">
<label class="form-check-label" for="checkbox1">
1
</label>
<input class="form-check-input" type="checkbox" value="" id="checkbox0">
<label class="form-check-label" for="checkbox0">
0
</label>
<label class="form-check-label" for="checkboxNull">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="checkboxNull">
Null
</label>
<input type="hidden" name='_method' value='POST'>
<input id="revisionFilterSubmit" type="submit" class='btn btn-danger btn-sm' value='Filter'>
</form>
My backend looks like the following:
routes:
Route::post('/revision/filter', 'RevisionController#filter')->name('revision.filter');
RevisionController:
public function filter(Request $request)
{
Log::info("Request: ");
Log::info($request);
return redirect()->route('revision.rindex');
}
My problem is that when I press the button I am getting redirected to:
The page has expired due to inactivity.
Please refresh and try again.
Instead I would like to see the request with the values of the checkboxes to implement the db saving functionality.
I appreciate your replies!
since I've already made a comment. I will put my answer in the answer box lol.
To answer you, you're not getting any checkbox value since you're checkboxes don't have any name attribute.
You need to put a name attribute to them. E.g.,
<input class="form-check-input" type="checkbox" value="" name="checkbox0" id="checkbox0">
From there, you should now be able to see the value of the checkbox via its name.
Just put same name to your checkbox.For example
<input class="form-check-input" type="checkbox" value="" id="checkbox1" name="revisionFilter">
<input class="form-check-input" type="checkbox" value="" id="checkbox2" name="revisionFilter">
I have a form with two text fields and two buttons (edit and delete), when I press edit button, it works fine but when I press delete button, it gives the 'MethodNotAllowedHttp' exception. My code is as follows:
<form action="/laboratory/doctors/update" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" aria-describedby="name" value="{{ $doctor->name }}">
</div>
<div class="form-group">
<label for="percentage">Percentage:</label>
<input type="text" class="form-control" id="percentage" value="{{ $doctor->percentage }}">
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
Delete
</form>
My Routes are as follows:
Route::post('/doctors/update', 'DoctorsController#update');
Route::delete('/doctors/{doctor}/delete', 'DoctorsController#destroy');
Any help is appreciated.
You have created route of http verbs as delete type and trying to get of type get. So you can change it as get instead delete
Route::get('/laboratory/doctors/{doctor}/delete', 'DoctorsController#destroy');
Also there is other way to make http verb as DELETE but using
Another form tag outside of current form tag.
Or use ajax to make it delete type
Href on an anchor html element will result in a GET call but your route expect a Delete call. You have some ways to make sure you will result in a delete call.
One of the most common ways is to use a form instead to post data to your server.
DELETE
{{ Form::open(['url' => '/laboratory/doctors/{{ $doctor->id }}/delete', 'method' => 'DELETE']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn btn-danger']) }}
{{ Form::close() }}
For best practise I recommend to use {{ Form::open(...) }} {{ Form::close() }} only once and refactor your controller code so it can read the value from the buttons and translate that in the corresponding {doctor} of the delete post so you dont have multiple html forms in your code.
#if(isset($doctor))
<form action="/laboratory/doctors/{{$doctor->id}}/delete" method="POST">
#else
<form action="/laboratory/doctors/update" method="POST">
#endif
{{ csrf_field() }}
#if(isset($doctor))
{{ method_field('DELETE') }}
#endif
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" aria-describedby="name" value="{{ $doctor->name }}">
</div>
<div class="form-group">
<label for="percentage">Percentage:</label>
<input type="text" class="form-control" id="percentage" value="{{ $doctor->percentage }}">
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
Delete
</form>
The problem is in your action.. when you click delete button, the button run the destroy function, but the form run the update function. So, you have to separate the form action, or you can delete it by using ajax
The oldvalue is usually set with the name attribute, in my case the name attribute is variable depending on the id name="option{{$question->id}}", so I can't find the way to save and load the olvalue, this is my code:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="option{{$question->id}}" id="question" autocomplete="off" value="1" data-oldvalue="{{old('option'.$question->id) }}"> Yes
</label>
<label class="btn btn-primary">
<input type="radio" name="option{{$question->id}}" id="question" autocomplete="off" value="0" data-oldvalue="{{old('option'.$question->id) }}"> No
</label>
</div>
Not sure if the If there is any other approach to get my oldvalues loaded (after validation errors) I would like to know.
Extra info: I'm not sure if the reason the old values don't load is the autocomplete="off". So I would like to know how to alert or get my oldvalue.
Set the values directly and use the default value if no old value is available.
value="{{ old('option' . $question->id, 1) }}"
value="{{ old('option' . $question->id, 0) }}"
In my create.blade.php I have the following code:
<div class="form-check">
<label for="active">active</label>
<input id="active" name = "active" type="checkbox" class="form-check-input" value="1">
</div>
And in my edit.blade.php the following:
<div class="form-check">
<label for="active">active</label>
<input id="active" name = "active" type="checkbox" class="form-check-input" value="{!!$company->
active!!}">
</div>
SQLFiddle of my db structure with data.
When submitting either with the checkbox checked they work fine
When editing a record with active set to 1, the checkbox isn't checked when opening the form
I know there's no value when not checked and when opening the checkbox isn't 'on' because the value is 1 instead of 'on'.
How can I resolve this problem?
It's actually an HTML problem, you are just using the input the wrong way, you have to use the checked instead of value:
<input
id="active"
name="active"
type="checkbox"
class="form-check-input"
checked="{{ $company->active ? 'checked' : '' }}"
>