I have setup a 4phases multiple dependent dropdown fields. I want to select region then country, owned or franchise and finally store name.
My issues: 1) Show/hide works OK but doesn’t reset values when I choose “-”. This mean that I can choose one store, then change my opinion, choose another option from upper field category and choose a second store. The POST will have both values. I want to reset the first value when I choose another option
2) Store names have all name attribute store_name. I can’t managed to make it record correctly on mysql db while I used implode().
jQuery:
$( document ).ready(function() {
$("#region").bind("change",function(){
var selectedValue=$(this).val().toLowerCase();
switch(selectedValue){
case "asia":
$("#country_asia").show();
$("#country_america").hide()
$("#country_europe").hide()
break;
case "america":
$("#country_asia").hide();
$("#country_america").show();
$("#country_europe").hide();
break;
case "europe":
$("#country_asia").hide();
$("#country_america").hide();
$("#country_europe").show();
break;
case "-":
$("#country_asia").hide()
$("#country_america").hide()
$("#country_europe").hide()
break;
}
});
$("#country_europe_dd").change(function()
{
var selectedValue=$(this).val().toLowerCase();
if(selectedValue == "greece")
{
$("#owned_gr").show();
}
else if(selectedValue == "-")
{
$("#owned_gr").hide();
}
})
$("#owned_gr_dd").change(function()
{
var selectedValue=$(this).val().toLowerCase();
if(selectedValue == "own")
{
$("#store_name").show();
$("#store_name_greece_fran").hide();
}
else if(selectedValue == "fran")
{
$("#store_name_greece_fran").show();
$("#store_name").hide();
}
else if(selectedValue == "-")
{
$("#store_name").hide();
$("#store_name_greece_fran").hide();
}
})
});
</script>
Form:
<div class="form-group region required" data-cid="region">
<label class="control-label" for="region">Region</label>
<div class="input-group"><span class="input-group-addon left"><i class="glyphicon glyphicon-pushpin"></i></span>
<select class="select form-control" onChange="displayForm(this)" id="region" name="region">
<option value="-">- Select -</option>
<option value="america">America</option>
<option value="asia">Asia</option>
<option value="europe">Europe</option>
</select>
</div>
</div>
<div id="country_europe" class="form-group country_europe required" style="display: none" data-cid="country_europe">
<label class="control-label" for="country">Country</label>
<div class="input-group"><span class="input-group-addon left"><i class="glyphicon glyphicon-pushpin"></i></span>
<select class="select form-control" id="country_europe_dd" name="country">
<option value="-">- Select -</option>
<option value="cyprus">Cyprus</option>
<option value="france">France</option>
<option value="germany">Germany</option>
<option value="greece">Greece</option>
<option value="iceland">Iceland</option>
<option value="italy">Italy</option>
<option value="netherlands">Netherlands</option>
<option value="norway">Norway</option>
<option value="serbia">Serbia</option>
<option value="switzerland">Switzerland</option>
</select>
</div>
</div>
<div id="owned_gr" class="form-group owned_gr required" style="display: none" data-cid="owned_gr">
<label class="control-label" for="owned_gr">Owned or Franchise ?</label>
<div class="input-group"><span class="input-group-addon left"><i class="glyphicon glyphicon-pushpin"></i></span>
<select class="select form-control" id="owned_gr_dd" name="owned">
<option value="-">- Select -</option>
<option value="own">Owned</option>
<option value="fran">Franchise</option>
</select>
</div>
</div>
<div id="store_name" class="form-group store_name required" style="display: none" data-cid="store_name">
<label class="control-label" for="store_name">Store Name - Greece (Owned)</label>
<div class="input-group"><span class="input-group-addon left"><i class="fa fa-buysellads"></i> </span>
<select class="form-control" name="store_name[]" id="store_name_gr_owned"
data-rule-required="true" >
<option value="-">- Select -</option>
<option value="option1">Option1</option>
<option value="option2">Option2</option>
</select>
</div>
</div>
<div id="store_name_greece_fran" class="form-group store_name_greece_fran required" style="display: none" data-cid="store_name_greece_fran">
<label class="control-label" for="store_name_greece_fran">Store Name - Greece (Franchise)</label>
<div class="input-group"><span class="input-group-addon left"><i class="fa fa-buysellads"></i> </span>
<select class="form-control" name="store_name[]" id="store_name_greece_fran"
data-rule-required="true" >
<option value="">- Select -</option>
<option value="athens">Athens</option>
<option value="crete">Crete</option>
</select>
</div>
</div>
PHP:
$store_name = implode($_POST['store_name']);
EDIT: From your comment below, sounds like solving #1 will also solve #2.
For #1, Whenever you hide
an element, set its value to -
as well. E.g.
case "asia":
$("#country_asia").show();
$("#country_america").hide();
$("#country_america").val("-");
$("#country_europe").hide();
$("#country_europe").val("-");
break;
Or, using vanilla JS:
document.getElementById("country_europe_dd").value = '-';
For #2, I can't see anything wrong with your code. Can you elaborate? Maybe include sample output of what you're seeing for $store_name
?
I found the solution.
I have to go one step further on jQuery and use val(). I reset other category values when i select specific values on the field i choose
$("#store_name_greece_fran_dd").change(function()
{
var selectedValue=$(this).val().toLowerCase();
if(selectedValue == "athens" || selectedValue == "athens" || selectedValue == "crete" || selectedValue == "crete")
{
$("#store_name_owned_dd").val('');
}
})