Ajax query returning correct value in console log, but not showing in the view - ajax

This is my html code.
<div class="form-group row add">
<label class="control-label col-sm-2" for="division_id">Division Name:</label>
<div class="col-sm-10">
<select name="district_id" id ="district_id" class="selectdistrict">
<option value="">--Select district--</option>
#foreach($atmdistrict as $cat)
<option value="{{$cat->id}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row add">
<label class="control-label col-sm-2" for="thana_id">Thana Name:</label>
<div class="col-sm-10">
<select name="thana_id" class="thananame" id ="thana_id" >
<option value="0" disabled="true" selected="true">Thana Name</option>
</select>
</div>
</div>
This is my ajax query for dependent dropdown.
$(document).ready(function(){
$(document).on('change','.selectdistrict',function(){
console.log("hmm its change");
var district_id=$(this).val();
console.log(district_id);
var div=$(this).parent();
var op=" ";
$.ajax({
type:'get',
url:'{!!URL::to('findThanaName')!!}',
data:{'id':district_id},
success:function(data){
console.log('success');
console.log(data);
//console.log(data.length);
op+='<option value="0" selected disabled>chose thana</option>';
for(var i=0;i<data.length;i++){
op+='<option value="'+data[i].id+'">'+data[i].name+'</option>';
console.log(data[i].name);
}
div.find('.thananame').html(" ");
// console.log( div.find('.name').html(" "));
div.find('.select').append(op);
//console.log( div.find('.select').append(op));
},
error:function(){
console.log('error');
}
});
});
});
The console log is showing no error, and is returning the correct data. However, the drop down menu is not showing any data. I can't figure out where I am going wrong.
screenshot of the program running

try to change :
div.find('.select').append(op);
by :
$("#thana_id").html(op);

Related

laravel dynamic drop down not giving value

im using ajax for laravel dropdown it was working but when i tiring to submit form it was giving id number
im getting right input in dropdown field when i try to submit page it was giving id number instead of dropdown value
i what to get value of MS OFFICE AND 1000 BUT IT STORING IN DATA BASCE {COURES TYPE C_1402:}
{ COURSE PRICE C_1402:}
my view
my network tab showing this id
my view page
<div class="col-md-4">
<div class="form-group">
<label for="location1">Course Type :<span class="danger">*</span> </label>
<select class="custom-select form-control required" name="student_course" id="student_courses" name="location" required>
<option value="">Select Course</option>
#foreach ($course_name as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="videoUrl1">Course Price :</label>
<select name="course_Price" id="course_Prices" class="form-control dynamic" data-dependent="course_Price">
<option value=""></option>
</select>
</div>
</div>
ajax
<script type="text/javascript">
$(document).ready(function() {
$('#student_courses').on('change', function() {
var stateID = $(this).val();
if(stateID) {
$.ajax({
url: '/Student_Course_get_price/'+stateID,
type: "GET",
dataType: "json",
success:function(data) {
$('#course_Prices').empty();
$.each(data, function(key, value) {
$('#course_Prices').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('#course_Prices').empty();
}
});
});
</script>
my controller
public function Student_Course_get()
{
$course_name = DB::table("courses")->pluck('COURSE_NAME','COURSE_NAME_id');
return view('Admin/Student.Student_enrollment',compact('course_name'));
}
public function Student_Course_get_price($COURSE_NAME_id)
{
$cities = DB::table("courses")
->where("C_id",$COURSE_NAME_id)
->pluck('COURSE_AMOUNT','C_id');
return json_encode($cities);
}
The value of your select option is a key not the value you see in the UI which will presumably be the 'C_id'.
Change your
$('#course_Prices').append('<option value="'+ key +'">'+ value +'</option>');
To
$('#course_Prices').append('<option value="'+ value +'">'+ value +'</option>');

ajax in laravel not showing. but in console , i can see the get url

I am making 2 dropdown which is the second one is dependent from first one. And here is my view code:
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="form-group">
<label class="form-label">General</label>
<select class="form-control formselect required"
placeholder="Select Category" id="sub_category_name">
<option value="0" disabled selected>Select
Main Category*</option>
#foreach($data as $categories)
<option value="{{ $categories->id }}">
{{ ucfirst($categories->catname) }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="form-group">
<label class="form-label">Sub</label>
<select class="form-control formselect required"
placeholder="Select Sub Category" id="sub_category">
</select>
</div>
</div>
Then here is my code in controller :
public function index(Request $request)
{
$data = DB::table('cats')->get();
return view('admin.genc.gencEntry')->with('data', $data);
}
public function subcat($id){
echo json_encode(DB::table('subcats')->where('catid', $id)->get());
}
And ajax is here:
<script>
$(document).ready(function () {
$('#sub_category_name').on('change', function () {
let id = $(this).val();
$('#sub_category').empty();
$('#sub_category').append(`<option value="0" disabled selected>Processing...</option>`);
$.ajax({
type: 'GET',
url: 'subcat/' + id,
success: function (response) {
var response = JSON.parse(response);
console.log(response);
$('#sub_category').empty();
$('#sub_category').append(`<option value="0" disabled selected>Select Sub Category*</option>`);
response.forEach(element => {
$('#sub_category').append(`<option value="${element['id']}">${element['subcatname']}</option>`);
});
}
});
});
});
</script>
But when i select a option from first dropdown, second one is not showing anything.
But i can see XHR finished loading: GET "http://www.example.com:8000/genc/subcat/7" in my console.
Can someone tell me where is the error causing the empty dropdown?
It's look like issue with your loop syntax, use it like
$.each(response, function(key,element) {
$('#sub_category').append(<option value="${element['id']}">${element['subcatname']}</option>);
});

how to show selected values in multi-select

I'm facing one issue about multi-select.
onchange i'm runnig ajax and on ajax success option appends to multiselect.
but at the time of edit i'm not able to trigger ajax and not able to show selected option in multiselect.
please check my code
form fleds
<div class="form-group {{ $errors->first('preferred_city', 'has-error') }}">
<label class="control-label col-lg-2">Preferred City
: </label>
<div class="col-lg-10">
<select name="preferred_city[]" class="form-control" id="preferred_city" multiple="multiple" style="color: #333">
#foreach($cities as $preferred_city)
#if(!empty($studentsDetails->preferred_city))
<option value="{{$preferred_city->id}}" {{ (in_array($preferred_city->id,\GuzzleHttp\json_decode($studentsDetails->preferred_city) )) ? 'selected' : '' }}>{{$preferred_city->city_name}}</option>
#else
<option value="{{$preferred_city->id}}">{{$preferred_city->city_name}}</option>
#endif
#endforeach
</select>
<span class="help-block">{{ $errors->first('preferred_city', ':message') }}</span>
</div>
</div>
<div class="form-group {{ $errors->first('collage_preferences', 'has-error') }}">
<label class="control-label col-lg-2">College Preferences
: </label>
<div class="col-lg-10">
<select id="collage_preferences" name="collage_preferences[]" class="form-control" multiple="multiple">
</select>
<span class="help-block">{{ $errors->first('collage_preferences', ':message') }}</span>
</div>
</div>
js
collage id's i'm fetching from database
var collage_pref = {!! $studentsDetails->collage_preferences !!}
I'm triggering preferred_city on page load
$(document).ready(function(){
if(studentsDetails !=null){
$('#preferred_city').trigger('change');
// $('#pic_file').trigger('change');
}
});
$('#preferred_city').on('change',function(){
$("#collage_preferences").multiselect('rebuild');
$.ajax({
url:'{{route('student.getInstituteslists')}}',
type:'get',
async: false,
data:{grad_course:$('#grad_course').val(),preferred_city:$('#preferred_city').val()},
success:function(e){
$('#collage_preferences').multiselect('refresh');
e.forEach(function(inst){
var data = '';
data +='<option value="'+ inst.id+'" >'+ inst.institute_name+'</option>';
$('#collage_preferences').append(data);
});
$('.collage_preferences').multiselect('rebuild');
}
});
});
$data = [];
$data['airlines'] = '';
$airlines = (Airline::select('Code','Name')->get())->toArray();
$myairlines = BlackoutAirline::where('SupplierId',$request->SupplierId)->pluck('AirlineCode')->toArray();
foreach($airlines as $airline) {
if(in_array($airline['Code'], $myairlines) ){
$data['airlines'] .= '<option value="'.$airline['Code'].'" selected>'.$airline['Name'].'</option>';
}
else{
$data['airlines'] .= '<option value="'.$airline['Code'].'">'.$airline['Name'].'</option>';
}
}
return json_encode($data);

Dropdown based on one table passing to other fields using ajax laravel

I'm having problem with regards to selected dropdown passing fields value to other input.
Basically I have one table (plans) which have id, plan_name, price. So i want to pass price to the other input.
So I expect the output that if I selected one of the choices the price will be on the price input.
blade.php
<div class="control-group">
<label class="control-label">Plan</label>
<div class="controls">
<select name="plan_id" id="plan_id" class="plan" style="width:220px">
<option value="0">Select</option>
#foreach($plans as $plan)
<option value="{{ $plan->id}}" >{{$plan->plan_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Price</label>
<div class="controls">
<input class="plan_price" type="number" name="price" id="price">
</div>
</div>
Here is the ajax i tried:
$(document).ready(function(){
$(document).on('change','.plan',function(){
//console.log("hmm its change");
var plan_id=$(this).val();
console.log(plan_id);
$.ajax({
type: 'get';
url:'{!!URL::to('findPrice')!!}',
data:{'id':plan_id},
dataType:'json',
success:function(data){
console.log("price");
console.log(data.price);
a.find('.plan_price').val(data.price);
},error:function(){
}
})
})
});
in my controller I have this function:
I dont know if this is also correct on the where part.
public function findPrice(Request $request){
$p = Plan::select('price')->where('id',$request->id)->first();
return response()->json($p);
}
Route::
Route::get('/findPrice','SubscriptionController#findPrice');

creating dynamic navigation sub menu select from database using Codeigniter?

I am trying to create a menu with submenu in codeigniter using ajax I successfully created the menu but i am facing the problem while i will create a submenu. When i click the menu I check it in cosole.log its showing controller is get the details but after that i dont know why its not working. Pls any one solve this issue. thanks in advance
This is My View Area
<form action="" method="post" id="frm_submenu">
<div class="form-group">
<label for="menu">Select Menu</label>
<select class="form-control" id="selectmenuid">
<option value="">-- Select Menu --</option>
<?php foreach($showData as $show):?>
<option value="<?php echo $show->menu_id?>"><?php echo $show->menu_name?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label for="menu">Select Sub Menu</label>
<select class="form-control" id="selectsubmenu">
<option value="">-- Select Submenu Menu --</option>
</select>
</div>
<div class="form-group">
<label for="imagetitle">Image Title</label>
<input type="text" class="form-control" name="imagetitle" id="imagetitle" placeholder="Enter Image Title" required="required">
</div>
<div class="form-group">
<label class="btn btn-default btn-file">
Browse <input type="file" style="display: none;">
</label>
</div>
<button type="submit" class="btn btn-primary" id="submit">Submit</button>
</form>
This is my ajax area
$("#selectmenuid").change(function() {
var selectmenuid = $(this).val();
console.log(selectmenuid);
$.ajax({
type: "POST",
data: selectmenuid,
url: "<?= base_url() ?>Admin_Creator/SelectSubmenudropdown",
success: function(data) {
$.each(data, function(i, data) {
$('#selectsubmenu').append("<option value='" + data.submenu_id + "'>" + data.submenu_name + "</option>");
});
}
});
});
This is my Controller area
public function SelectSubmenudropdown()
{
if(isset($_POST['selectmenuid']))
{
$this->output->set_content_type("application/json")->set_output(json_encode($this->Model_Creator->getSubmenu($_POST['selectmenuid'])));
}
}
This is my Model area
public function getSubmenu(){
$this->db->select('submenu_id,submenu_name');
$this->db->from('menu,submenu');
$this->db->where('submenu.menu_id=menu.menu_id');
$this->db->where('submenu.menu_id', $selectmenuid);
return $query->result_array();
}
You have to receive $selectmenuid in your model method:
public function getSubmenu($selectmenuid){
$this->db->select('submenu_id,submenu_name');
$this->db->from('menu,submenu');
$this->db->where('submenu.menu_id=menu.menu_id');
$this->db->where('submenu.menu_id', $selectmenuid);
return $query->result_array();
}

Resources