So I want to refresh the update form with the change in the select option (which will be year).
This is my form:
<select class="form-control" id="year_select" name="year_select">
#foreach($pickyear as $key=>$unique)
<option value= "{!! $unique !!}"> {!! $unique !!} </option>
#endforeach
</select>
{!! Form::open(['url' => 'form', 'class' => 'form-horizontal']) !!}
//form is included here
{!! Form::close() !!}
So I want to reload or refresh the form with every change in select value or ajax call.
Ajax call:
$('select[name="year_select" ]).change(function(e) {
// e.preventDefault();
const year = $(this).val();
window.location.reload();
const url = `/form/${year}`;
$.ajax({
url,
type: 'PUT',
success: function(res) {
window.location.reload();
}
});
})
Edit: Refreshing / reloading whole page would do too.
Update: The imput parameter should be updatable and should change value according to the year from select option.
Use onchange="this.form.submit()" for page reload when Select Option change
on blade
<select name="year_select" onchange="this.form.submit()">
#foreach($pickyear as $key=>$unique)
<option value= "{!! $unique !!}"> {!! $unique !!} </option>
#endforeach
</select>
now url should be form_url?year_select=2022 and you can filter data from controller
On controller
$model = Model::query();
if (request('year_select')) {
$model->whereYear('your_year_column', request('year_select'));
}
// else
// {
// // if you need current year as default
// $model->whereYear('your_year_column', date('Y'));
// }
$data = $model->get();
Related
I have two dropdown list in my Laravel page. I am trying to get old value of the dependent dropdown list (categorylist.blade) and make it selected once I fill and post the data. It returns back to the same page if the validation has not successfully completed. I am able to get all values except this one. I have tried Session::put() as well as Session::flash() but did not work. The category list is retrieved once you chose section as it requests the category list from the controller through ajax. How can I get the old value in the category dropdown list after the page refreshed.
Here is my section selection dropdown:
<select class="form-control sectionchoose" name="section_id" id="section_id">
<option value="">Choose Section</option>
#foreach($sections as $section)
<option value="{{$section['id']}}" #if(old('section_id') == $section['id']) selected #endif>{{$section['name']}} </option>
#endforeach
</select>
My categorylist dropdown:
<label class="col-sm-6">Chose Category</label>
<div class="categorylist">
#include('admin.deal-management.categorylist')
</div>
And this is my categorylist view file:
<select class="form-control " name="category_id" id="category_id">
<option value="">Choose Category</option>
#foreach($categories as $category)
<option value="{{$category['id']}}" #if(old('category_id') == $category['id']) selected #endif>
{{$category['category_name']}}
</option>
#endforeach
</select>
and this is my main controller:
public function addEditDeals(DealAddEditRequest $request, $id=null){
//*** post starts here ***/
if($request->isMethod('post')){
$message = 'Updated successfully';
$data=$request->all();
$deal->fill($request->validated());
$deal->save();
return redirect()->back()->with($message);
}
This is my categorylist controller:
public function findCategories(Request $request){
if($request->json()){
$data = $request->all();
$categories = Category::where(['section_id' => $data['id'], 'status'=>1])->get()->toArray();
return view('admin.deal-management.categorylist',compact('categories'));
}
}
And finally, this is the jQuery part:
$(document).ready(function (){
let sectionid = $('.sectionchoose').val();
$.ajax({
headers: {
'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
},
type: 'POST',
datatype: 'json',
url : '/admin/selectsection',
data: {id:sectionid},
success: function(response){
$('.categorylist').html(response)
}, error:function(){
}
})
})
Finally, I was able to find the solution. Whoever has the same issue can use the approach below:
Step 1: Send the Session value through the with() command:
return redirect()->back()->withErrors($validator)
->withInput()->with('cat_id',$data['category_id']);
}
Step 2: Retreive the data in your main blade and attain hidden input:
<input class="cat_id" id="asd" type="hidden" value="{{Session::get('cat_id')}}"/>
Step 3: Get the retreived session value in Jquery:
$(document).ready(function (){
let sectionid = $('.sectionchoose').val();
let cat_id;
if($('#asd').val()){
cat_id = $('#asd').val();
} else {
cat_id = 0;
}
$.ajax({
headers: {
'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
},
type: 'POST',
datatype: 'json',
url : '/admin/selectsection',
data: {id:sectionid, cat_id:cat_id},
success: function(response){
$('.categorylist').html(response)
}, error:function(){
}
})
})
Step 4: Send the session value to your dependent blade again (as cat_id here)
public function findCategories(Request $request){
if($request->json()){
$data = $request->all();
$cat_id = $data['cat_id'] ?? '';
$categories = Category::where(['section_id' => $data['id'], 'status'=>1])->get()->toArray();
return view('admin.deal-management.categorylist',compact('categories','cat_id'));
}
}
Done! As far as I know, there is not any other way to get old value of dependent dropdown list value so far.
8
I have a form and I used {!! csrf_field()!!}
I have 2 select element one is for state and one is for the city
when state select change a ajax send and give new cities
if this ajax request doesn't execute we don't have a problem
but if ajax request send then form submit we give 419 page expired error
this is ajax code
$('#state').change(function () {
var state_id = $(this).val();
$.ajax({
type: 'get',
url: '/panel/selectCitiesByStateId/' + state_id,
data: {'nothing': 'nothing'},
timeout: 25000,
error: function () {
alert('sorry error');
},
success: function (res) {
res = JSON.parse(res);
var entires = Object.entries(res);
var htmlOptionElements = '';
for (var i = 0; i < entires.length; i++) {
var city = entires[i];
htmlOptionElements += '<option value="' + city[1] + '">' + city[0] + "</option>";
}
$('#city_id').html(htmlOptionElements);
}
})
});
this is the form top codes
<form action="{{route('admin.job.update',['job'=>$job->slug])}}"
method="post" enctype="multipart/form-data" id="jobForm">
{!! csrf_field() !!}
{!! method_field('patch') !!}
this is select codes
<div class="col-sm-6">
<label for="" class="control-label">state*</label>
<select name="state_id" id="state" class="form-control">
#foreach($states as $state)
<option value="{{$state->id}}"
#if($state->id==#$job->state_id)
selected
#endif
>{{$state->name}}</option>
#endforeach
</select>
</div>
<div class="col-sm-6">
<label for="" class="control-label">city*</label>
<select name="city_id" id="city_id" class="form-control">
#foreach($job->state->cities as $city)
<option value="{{$city->id}}"
#if(#$job->city->id==$city->id)
selected
#endif
>{{$city->name}}</option>
#endforeach
</select>
</div>
well,
one csrf token can be used once. and csrf token generate by a get request.
when you load the form by a get request, token generated and filled the form csrf field.
when the ajax fired on change of #state, you are sending a get request to the system. that means a new csrf token will be generated.
Thats why when you send the request (You already send get request by ajax), you got 419 error.
You can solve this problem by sending post request to ajax for #state, and disable csrf check for the route.
This May solve Your Problem.
Im having trouble to retrieve data into input box right after user select from dropdown. Any suggestion? Last suggest was not working.
internalaudit.blade.php
<div class="form-group">
{!! Form::label('text', 'Doc No', ['class' => 'col-lg-3 control-label']) !!}
<div class="col-lg-10">
<select name="docNo" id="docNo" class="form-control" style="width:250px">
#foreach ($soplists as $soplist)
<option value="{{ $soplist->id }}">{{ $soplist->doc_no }}</option>
#endforeach
</select>
</div>
</div>
<input type="text" class="form-control" name="rev_no" id="rev_no">
<input type="text" class="form-control" name="title" id="title">
Ajax
<script>
$('#docNo').change(function() {
var id = $(this).val();
var url = '{{ route("getDetails", ":id") }}';
url = url.replace(':id', id);
$.ajax({
url: url,
type: 'get',
dataType: 'json',
success: function(response) {
if (response != null) {
$('#rev').val(response.rev_no);
$('#title').val(response.title);
}
}
});
});
Controller
public function internalAudit()
{
/*Generate number*/
$query = Cars::latest()->first(); //get last query
$validators = User::all();
$departments = Department::all();
$isolists = isoList::all();
$soplists = sopList::all();
$ex = explode('/', $query['iaCarRefNo']); //explode last number from DB
$type = strtoupper(Request::segment(2)); //get type from url
if (empty($query->iaCarRefNo)) {
$number = '1';
$nextNumber = 'PLW' . '/' . date('y') . $type . '/' . sprintf("%03d", $number);
} else {
$number = $ex[2] + 1;
$nextNumber = 'PLW' . '/' . date('y') . $type . '/' . sprintf("%03d", $number);
}
return view('cars.internalaudit', compact('nextNumber', 'validators', 'departments', 'isolists', 'soplists'));
}
public function getDetails($id = 0)
{
$data = sopList::where('doc_no', $id)->first();
return response()->json($data);
}
Route
Route::get('get/details/{id}', 'internalAuditController#getDetails')->name('getDetails');
Route::get('/internalaudit', 'internalAuditController#internalAudit');
Route::post('/internalaudit', ['as' => 'internalaudit.store', 'uses' => 'internalAuditController#store']);
Database sop_list table image link
https://ibb.co/SwkJhLc
Dropdown and input image
https://ibb.co/0VN3Z2y
Network tab
https://ibb.co/56w5BLD
Accoding to your console.log there are 2 errors $.ajax is not function and $.(...) datetimepicker is not function beacuse of those errors, now your ajax does not work therefore you need to fix those issues first. Once you fix those issues you will be able to send ajax request and update input fileds.
To fix first problem, download the regular (compressed or not) version of jQuery and include it in your project.
To fix second problem, download relevant datetimepicker and include in your project same like jQuery.
After that update #rev selector to #rev_no
$('#rev_no').val(response.rev_no);
Also use laravel helper function instead echo json_encode();
return response()->json($data);
In your table there is no record for doc_no = 3, in your blade you're using $soplist->id
<option value="{{ $soplist->id }}">{{ $soplist->doc_no }}</option>
So do the same thing on controller,
$data = sopList::where('id', $id)->first();
Or
$data = sopList::find($id);
You gave a wrong id to response, also you should give an id to title input.
<input type="text" class="form-control" name="title" id="title">
<script>
$(document).ready(function(){
$(document).on('change','#docNo',function(){
var id = $(this).val();
var url = '{{ route("getDetails", ":id") }}';
url = url.replace(':id', id);
$.ajax({
type: 'get',
url: url,
dataType: 'json',
success: function(response) {
$('#rev_no').val(response.rev_no);
$('#title').val(response.title);
}
});
});
});
</script>
I'm trying to use the data of the first dropdown, to fill the second.
An example here:
https://gitlab.com/Bons/laravel5.3_dynamic_dropdown/blob/master/readme.md
Controller functon return data, but second dropdown is empty.
First dropdown:
<span>Country: </span>
<select style="width: 200px" class="country" id="id_country">
<option value="0" disabled="true" selected="true">-Select-</option>
#foreach($countries as $contry)
<option value="{{$contry->id_country}}">{{$contry->name}}</option>
#endforeach
</select>
linked dropdown:
<label>City
<select id="region" class="form-control input-sm" name="region_id">
<option value=""></option>
</select>
</label>
Script:
<script type="text/javascript">
$(document).ready(function(){
$(document).on('change','.country',function(){
console.log("hmm its change");
var id=$(this).val();
console.log(id)
$.ajax({
type:'get',
url:'{!!URL::to('findRegions')!!}',
data:{'id':id},
success:function(data) {
console.log("success");
console.log(data.length);
console.log(data);
$.each(data, function(index,subCatObj){
// console.log(subCatObj.name)
$('#region').append(''+subCatObj.name+'');
});
},
error:function(){
console.log("error");
}
});
});
});
</script>
Route:
Route::get('/findRegions/','GirlsController#findRegions');
Controller function:
public function findRegions(Request $request){
$id=$request->id;
$regions=Region::select('name')
->where('id_country',$id)
->get();
dump($regions);
return $regions;
}
Here is how i did it,
in routes/web.php
Route::post('select-ajax', 'Main#selectAjax')->name('select-ajax');
in Controller : Main.php
public function __construct()
{
View::share('selectAllCountries', Country::pluck("name","id")->all());
}
public function selectAjax(Request $request)
{
if($request->ajax())
{
$getRegion = Region::where('id_country',$request->id)->pluck("nameOfTheRegion","id")->all();
$data = view('ajax-select',compact('getRegion'))->render();
return response()->json(['options'=>$data]);
}
}
then create a simple blade file name ajax-select.blade.php
<option selected disabled>--- Select Region---</option>
#if(!empty($getRegion))
#foreach($getRegion as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
#endif
Ajax call
{!! Form::select('country',[' '=>'--- Select Country ---']+$selectAllCountries,null,['class'=>'form-control']) !!}
{!! Form::select('region',[' '=>'--- Select Region---'],null,['class'=>'form-control']) !!}
<script>
$("select[name='country']").change(function(){
var id = $(this).val();
var token = $("input[name='_token']").val();
$.ajax({
url: "{{ route('select-ajax') }}",
method: 'POST',
data: {'id':id, '_token':token},
success: function(data) {
$("select[name='region']").html('');
$("select[name='region']").html(data.options);
},
error:function(){
alert("error!!!!");
}
});
});
</script>
Hope this helps
I have a myitems($filter) method in a ItemsController that loads the myitems.blade view.
The view has list of items as label and text box to get number of items ($items is passed from myitems method to myitems view and there is a model form binding on items model as well)
I have a select box that has options 1. All, 2. Expired and 3.New
When I change the selection I want to have get new $items from the controller and recreate the form with new items list.
I am using using jquery to alert that the selection is changed. but I am not able to figure out how will I be able to call myitems($filter) from ajax and redirect to create the items.blade page.
Route
//Note the ? in parameter, it says parameter is optional
Route::get('/myitems/{filter?}',
['as' => 'myitems.list', 'uses' => 'ItemController#myitems']
);
Controller
...
public function myitems(Request $request, $filter = null)
{
if (empty($filter)) $filter=1;
$items = Item::where('item_type', $filter)->get();
// if ajax call just return the partial that displays the list
if ($request->ajax()) {
return view('_itemlist', compact('items'));
}
// passed as options to input select
$filters= [
'All' => '0',
'New' => '1',
'Expired' => '2'
];
return view('itempagewith_defaultitemlist', compact('items','filters'));
}
...
View
view itempagewith_defaultitemlist.blade.php
<div class="container">
{!! Form::model($myitems, ['route' => 'myitems.list', 'class'=>'form-horizontal', 'files' => true]) !!}
<div id="filterselectbox">
<div class="form-group">
{!!Form::label('filterselectbox','*Filter:',['class'=>'control-label']) !!}
{!!Form::select('filterselectbox', $filters, null, ['class'=>'form-control ']) !!}
</div>
</div>
{!! Form::close() !!}
<div id="item-container">
#include('_itemlist')
</div>
<script>
$('#filterselectbox').change(function() {
var id = $('#filterselectbox').val();
var ajaxurl = '{{route('myitems', ':id')}}';
ajaxurl = ajaxurl.replace(':id', id);
$.ajax({
url: ajaxurl,
type: "GET",
success: function(data){
$data = $(data); // the HTML content that controller has produced
$('#item-container').hide().html($data).fadeIn();
}
});
});
</script>
</div>
partial view _itemlist.blade.php
<ul>
#foreach ($items as $item)
<li>{{ $item->name }}</li>
#endforeach
</ul>