I have this code select state -> show state cities :
<div class="form-group">
{{ Form::label('state', 'State') }}
<select name="state" id="state" class="form-control">
<option value="">{{ __('Select Province') }}</option>
#foreach ($state->data as $info)
<option value="{{$info->province_id}}">{{$info->province}}</option>
#endforeach
</select>
</div>
<div class="form-group">
{{ Form::label('city', 'City') }}
<select class="form-control" name="city" id="city">
<option value="">{{ __('Select City') }}</option>
</select>
</div>
and this javascript:
<!-- find cities -->
<script type="text/javascript">
$(document).ready(function() {
$('select[name="state"]').on('change', function() {
var provinceID = $(this).val();
if(provinceID) {
$.ajax({
url: '{{ url('admin/getcitylistshipping') }}/'+encodeURI(provinceID),
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="city"]').empty().append("<option value='' selected>Select</option>");
$.each(data.data, function(key, value) {
$('select[name="city"]').append('<option value="'+ value['city_name'] +'">'+ value['type'] + ' - ' + value['city_name'] +'</option>');
});
}
});
}else{
$('select[name="city"]').empty().append("<option value='' selected>Select</option>");
}
});
});
</script>
What I want is to save options text in my database instead of their
value (which is ID).
what I did
As my cities list comes by json and nothing depended on them I changed my values in my script from value="'+ value['city_id'] + to value="'+ value['city_name'] + and I get my city names instead of their id's.
But my issue is with my states (provinces) part, in that part I need to have state id as value in order to return city names but i don't want to save that id in my database, what i want is to save option name.
Here is my store method:
$this->validate($request, array(
'state' => 'nullable',
));
$shipping->state = $request->input('state');
how can I save my selected state text instead of their id in my
controller?
ideas?
SOLVED
I added extra hidden input to my blade and changed my script to:
html
<div name="statehidden" id="statehidden"></div>
script
<script type="text/javascript">
$(document).ready(function() {
$('select[name="state"]').on('change', function() {
var provinceID = $(this).val();
var statename = $("select[name='state'] option:selected").text(); //add this
if(provinceID) {
$.ajax({
url: '{{ url('admin/getcitylistshipping') }}/'+encodeURI(provinceID),
type: "GET",
dataType: "json",
success:function(data) {
$('#statehidden').empty(); //add this
$('#statehidden').append("<input type='text' name='statehidden' id='statehidden' value='"+statename+"'>"); //add this
$('select[name="city"]').empty().append("<option value='' selected>Select</option>");
$.each(data.data, function(key, value) {
$('select[name="city"]').append('<option value="'+ value['city_name'] +'">'+ value['type'] + ' - ' + value['city_name'] +'</option>');
});
}
});
}else{
$('#statehidden').empty(); //add this
$('select[name="city"]').empty().append("<option value='' selected>Select</option>");
}
});
});
</script>
Related
I am new to Ajax. So, excuse my lack of knowledge. When I pass the data in the view, I get IDs in my database. Even though I passed text from a dropdown dependent menu is that possible with explanation!
Please see those 2 tables category
and products
I wanted to get the names from category ('Désignation' => first table) in my view - but instead I got the IDs. Please help.
controller
public function create() {
$prod=ProductCat::all();
return view('Achats.create',compact('prod'));
}
public function findProductName(Request $request) {
$data = Product::select('Désignation','id') -> where('catégorie', $request->id) -> take(100) -> get();
return response()->json($data);
}
public function findRef(Request $request) {
$p = Product::select('Référence')->where('id',$request->id)->first();
return response()->json($p);
}
Routes
Route::get('create','AchatController#create')->name('create.Achat');
Route::get('/findProductName','AchatController#findProductName');
Route::get('/findRef','AchatController#findRef');
View
<form action="{{ route('Achat.store')}}" method="POST" enctype="multipart/form-data">
<span>Product Category: </span>
<select style="width: 200px"class="form-control catégorie" name="catégorie" id="catégorie" >
<option value=" " disabled="true" selected="true">-Select-</option>
#foreach($prod as $cat)
<option value="{{$cat->id}}">{{$cat->Désignation}}</option>
#endforeach
</select>
<span>Product Name: </span>
<select name ="Désignation" class="form-control Désignation" id="Désignation" style="width: 200px" >
<option value="" disabled="true" selected="true">Product Name</option>
</select>
<span>Product ref: </span>
<input name="Référence" id="Référence" class="form-control Référence" type="text" >
<br>
ajax
<script type="text/javascript">
$(document).ready(function(){
$(document).on('change','.catégorie',function(){
// console.log("hmm its change");
var cat_id=$(this).val();
// console.log(cat_id);
var div=$(this).parent();
var op=" ";
$.ajax({
type:'get',
url:'{!!URL::to('findProductName')!!}',
data:{'id':cat_id},
success:function(data){
//console.log('success');
//console.log(data);
//console.log(data.length);
op+='<option value="0" selected disabled>chose product</option>';
for(var i=0;i<data.length;i++){
op+='<option value="'+data[i].id+'">'+data[i].Désignation+'</option>';
}
div.find('.Désignation').html(" ");
div.find('.Désignation').append(op);
},
error:function(){
}
});
});
$(document).on('change','.Désignation',function () {
var prod_id=$(this).val();
var a=$(this).parent();
console.log(prod_id);
var op="";
$.ajax({
type:'get',
url:'{!!URL::to('findRef')!!}',
data:{'id':prod_id},
dataType:'json',//return data will be json
success:function(data){
console.log("Référence");
console.log(data.Référence);
// here price is column name in products table data.coln name
a.find('.Référence').val(data.Référence);
},
error:function(){
}
});
});
});
It looks like you are passing the Id as the value, hence the reason for getting the Id in your database table.
Change
#foreach($prod as $cat)
<option value="{{$cat->id}}">{{$cat->Désignation}}</option>
#endforeach
To
#foreach($prod as $cat)
<option value="{{$cat->Désignation}}">{{$cat->Désignation}}</option>
#endforeach
That should help.
I have three tables like tbl_customers, tbl_items, tbl_customersOrders. I inserted the tbl_customers ID and tbl_items ID into the tbl_customersOrders which as a foreign key. Now think is based on first drop-down lists (here the tbl_customersOrders ID) I can fetch another two fields on other drop-down lists but .... I need the name of the customers which is id matched...also items name which is id matched Like:
what I tried:
Controller Code:
$data['gets_order'] = $this->box_property_model->getCustDataOrder();
Model Code:
public function getCustDataOrder()
{
$this->db->select('*');
$this->db->order_by('customer_orders_id', 'DESC');
$query = $this->db->get('tbl_customer_orders');
return $query->result();
}
View code:
<select name="nestedOrder" class="form-control" >
<option value="">--- Select Order ---</option>
<?php if(!empty($gets_order)) {
foreach($gets_order as $nst){
?>
<option value="<?php echo $nst->customer_orders_id; ?>"><?php echo $nst->new_id_generated; ?></option>';
<?php } } ?>
</select>
<label for="custNested">Customer Name-</label>
<select name="custNested" id="custNested" class="form-control" >
<option value="">Select ITEMS ID</option>
</select>
<label for="ItemsName">Items Name</label>
<select name="items_ids" id="getsitemsName" class="form-control" >
<option value="">Select ITEMS</option>
</select>
<label for="testing">Final ITEM Name</label>
<select name="org_itemsNameValue" id="org_itemsNameValue" class="form-control" >
<option value="">Select ITEMS Name</option>
</select>
JS CODE:
<script type="text/javascript">
$(document).ready(function() {
$('select[name="nestedOrder"]').on('change', function() {
var customID = $(this).val();
alert(customID);
$.ajax({
url: '<?php echo base_url();?>/Box_property/DropdownsAjaxNested/'+customID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="custNested"]').empty();
$('select[name="custNested"]').append('<option value="0">--Select CUSTTT--</option>');
$.each(data, function(key, value) {
$('select[name="custNested"]').append('<option value="'+value.customer_id + '">'+ value.customer_name +'</option>');
});
}
});
$.ajax({
url: '<?php echo base_url();?>/Box_property/DropdownsAjaxNestedItems/'+customID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="items_ids"]').empty();
$('select[name="items_ids"]').append('<option value="0">--Select --</option>');
$.each(data, function(key, value) {
$('select[name="items_ids"]').append('<option value="'+value.items_id +'">'+ value.items_id +'</option>');
});
}
});
});
$('select[name="items_ids"]').on('change', function() {
var items_ids = $(this).val();
alert(items_ids);
$.ajax({
url: '<?php echo base_url();?>/Box_property/itemsOrdersIDfetch/'+items_ids,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="org_itemsNameValue"]').empty();
$('select[name="org_itemsNameValue"]').append('<option value="0">--Select ITEMS Name--</option>');
$.each(data, function(key, value) {
$('select[name="org_itemsNameValue"]').append('<option value="'+value.items_id + '">'+ value.items_name +'</option>');
});
}
});
});
});
</script>
In Controller:
public function DropdownsAjaxNested($id) {
$result = $this->db->where("customer_id",$id)->get("tbl_customers")->result();
echo json_encode($result);
}
public function DropdownsAjaxNestedItems($id) {
$result = $this->db->where("customer_orders_id",$id)->get("tbl_items_order")->result();
echo json_encode($result);
}
public function itemsOrdersIDfetch($id){
$result = $this->db->where("items_id",$id)->get("tbl_items_master")->result();
echo json_encode($result);
}
how can i use a set_select option on ajax part for a dynamic dependent dropdown list, this list is clearing after validation errors, so i want to use set_select option here. kindly see below code:
<script type="text/javascript">
$(document).ready(function() {
$('select[name="relegion"]').on('change', function() {
var regID = $(this).val();
if(regID) {
document.write("ok");
$.ajax({
url: '/demo/main/selectcaste/'+regID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="caste"]').empty();
$('select[name="caste"]').append('<option value=1>'+ "Not Interested to specify" +'</option>');
$('select[name="caste"]').append('<option value=2>'+ "InterCaste" +'</option>');
$.each(data, function(key, value) {
$('select[name="caste"]').append('<option value="'+ value.id +'">'+ value.name +'</option>');
});
}
});
}else{
$('select[name="caste"]').empty();
}
});
});
Maybe you need something like this,
View code.
<select name="category" id="category">
<option>Pilih Kategori Kelas</option>
<option value="1">category 1</option>
<option value="2">category 2</option>
</select>
<select name="sub_category" id="sub_category">
<option>Pilih category Kelas</option>
</select>
AJAX code.
$(document).ready(function(){
$('#category').on('change',function(){
var category_id = $(this).val();
if(category_id){
$.ajax({
type:'POST',
url:'<?php echo base_url()."profile_kelas/get_sub_category";?>',
data: {
category_id : category_id
},
success:function(html){
$('#sub_category').html(html);
}
});
}else{
$('#sub_category').html('<option>Pilih Sub category Kelas</option>');
}
});
});
My Controller code.
public function get_sub_category(){
$data = $this->model_profile->get_sub_category($_POST['category_id']);
foreach ($data as $row) {
echo '<option value="'.$row->id.'">'.$row->sub_category.'</option>';
}
}
You can make your parent category dynamically.
i use jquery to append option to select
it's worked but i want add
<option value="0">choose category</option>
after any selected
when i add it in html it's hidden after select option apeend
<script type="text/javascript">
$(document).ready(function() {
$('select[name="cat"]').on('change', function() {
$('select[name="subcat"]').removeClass('hidden');
$('select[name="subcat2"]').addClass('hidden');
//$('select[name="subcat2"]').find('option').remove().end();
var stateID = $(this).val();
if(stateID) {
$.ajax({
url: '{{ url('getCat') }}/'+stateID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="subcat"]').empty();
$.each(data, function(key, value) {
$('select[name="subcat"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('select[name="subcat"]').empty();
}
});
});
That's because you are calling empty function before appending. So if you have hard coded option <option value="0">choose category</option> it will be removed before new options are added.
success:function(data) {
$('select[name="subcat"]').empty();
$('select[name="subcat"]').append('<option value="0">choose category</option>');
$.each(data, function(key, value) {
$('select[name="subcat"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
I made a simulation of your problem, see if that's what you want?
Link jsfiddle
html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select name="cat">
<option value="0">choose category</option>
<option value="1">Cat 1</option>
<option value="2">Cat 2</option>
</select>
<select name="subcat" style="display:none">
</select>
JS
$(document).ready(function() {
$('select[name="cat"]').on('change', function() {
//$('select[name="subcat"]').removeClass('hidden');
//$('select[name="subcat2"]').addClass('hidden');
//$('select[name="subcat2"]').find('option').remove().end();
var stateID = $(this).val();
if(stateID) {
$.ajax({
data: {
json: JSON.stringify({
object: {
1: 'Sub cat 1',
2: 'Sub cat 2',
3: 'Sub cat 3',
}
}),
delay: 1
},
url: '/echo/json/',
type: "POST",
dataType: "json",
success:function(data) {
$('select[name="subcat"]').empty();
$('select[name="subcat"]').append('<option value="0">choose category</option>');
$.each(data.object, function(key, value) {
$('select[name="subcat"]').append('<option value="'+ key +'">'+ value +'</option>');
});
$('select[name=subcat]').show();
}
});
}else{
$('select[name="subcat"]').empty();
}
});
});
I have scenario form like this
if option selected it will come out a new field i named "hah" i call it using ajax script like this
$('#bahan').change(function() {
var id = $('#bahan').val()
var datana = 'id=' + id
var urlna = "<?=base_url()?>controller/food/getdetailstok";
$.ajax({
type: 'POST',
url: urlna,
data: datana,
success: function(data) {
$('#hah').html(data);
}
})
return false;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<select class="form-control select2" name="bahan" id="bahan">
<option value="">-- Choose One --</option>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
</select>
</div>
<div id="hah"></div>
How to reset "hah" if i click button save. hah in reset because the value field options again become null
$('#hah').empty() will do the trick.
.empty() will remove every element from the DOM. In your case every element in your #hah div.
$('#bahan').change(function() {
$('#hah').html("");
var id = $('#bahan').val()
var datana = 'id=' + id
var urlna = "<?=base_url()?>controller/food/getdetailstok";
$.ajax({
type: 'POST',
url: urlna,
data: datana,
success: function(data) {
$('#hah').html(data);
}
})
return false;
})