The problem : I implemented a simple display:none | display:block code which on selection is showing up relevant fields depending on the option selected. When I try to submit the information after the selection using a form and JQuery(as a handler), the values sent to the handler are empty.
My implementation:
<form id="InfoForm" method="POST">
<!-- Here there are other few fields but these are being sent correctly. -->
<select id="country" class="form-control" onchange="change()">
<option value="NL">Netherlands (NL)</option>
<option value="NZ">New Zealand (NZ)</option>
<option value="MK">North Macedonia (MK)</option>
<option value="US">United States Of America (US)</option>
<!-- ....other countries -->
</select>
<script>
function change() {
d = document.getElementById("country").value;
if(d == 'MK' || d == 'NL' || d == 'MZ') {
document.getElementById("specific-formgroup3").style.display = "block";
document.getElementById("specific-formgroup2").style.display = "none";
document.getElementById("specific-formgroup4").style.display = "none";
document.getElementById("specific-formgroup5").style.display = "none";
document.getElementById("specific-formgroup6").style.display = "none";
//and so on ....
} else if (d == 'US') {
$('#specific-formgroup2').show();
$('#specific-formgroup3').hide();
$('#specific-formgroup4').hide();
$('#specific-formgroup5').hide();
$('#specific-formgroup6').hide();
//and so on ....
// Here I tried to solve the problem using this JQuery implementation, it still is not working.
// Of course the if else list continues but I don't think I need to paste it here since it is too
// big. Otherwise I checked the syntax and everything is fine with the code following the one above.
}
}
</script>
<div id="specific-formgroup3" style="display: none;">
<div class="form-group">
<label>Specific Number</label>
<input type="text" class="form-control" id="som_num" />
</div>
<div class="form-group">
<label>Account number</label>
<input type="text" class="form-control" id="som2_num" />
</div>
</div>
<div id="specific-formgroup4" style="display: none;">
<div class="form-group">
<label>Other Number</label>
<input type="text" class="form-control" id="som_num" />
</div>
<div class="form-group">
<label>Account Number</label>
<input type="text" class="form-control" id="som2_num" />
</div>
</div>
<div id="specific-formgroup5" style="display: none;">
<div class="form-group">
<label>Institution number</label>
<input type="text" class="form-control" id="som_num" />
</div>
<div class="form-group">
<label>Account Number</label>
<input type="text" class="form-control" id="som2_num" />
</div>
</div>
<div id="specific-formgroup6" style="display: none;">
<div class="form-group">
<label>Code Number</label>
<input type="text" class="form-control" id="som_num" />
</div>
<div class="form-group">
<label>Account Number</label>
<input type="text" class="form-control" id="som2_num" />
</div>
</div>
<div id="specific-formgroup2" style="display: none;">
<div class="form-group">
<label>Transit number</label>
<input type="text" class="form-control" id="som_num" />
</div>
<div class="form-group">
<label>Account Number</label>
<input type="text" class="form-control" id="som2_num" />
</div>
</div>
</form>
After submiting the form with the fields above to the a JQuery handler, the request is sending empty values for no reason. I used different brwosers and even change the style.display = "none" to visibility = "hidden"; but still the handler is receiving empty values.
Important: When I remove the specific-formgroups and their inner content, leaving only one left, the form is working fine.
I tried to solve the problem using this JQuery implementation, it still is not working. (seen above)
The handler code is all working good , so there is no error coming from the handler itself. The values are just sent empty.
2
Answers
You can use below code to show one element and hide all other elements. here i used ids of form group elements same as country names. this will add some dynamic handling.
Remove the "display: none" styling for the first element to show that element in initial loading.
If you want multiple Countries to show same form group, you can maintain common value for those country options and give that value as id for the respective form group, like what i did for Netherlands and New Zealand.
Why not use the build-in functionality in the form itself? Instead of using div elements to show and hide parts of the form you can use the Field Set element. It has an attribute called
disabled
. It will not hide the element, but you can style it not to show. The primary advantage is that the data entered in the input elements inside the disabled fieldset will not be part of the submitted form. Try select NL, enter date, switch to US, enter data and then press submit.And then, please do not use id’s all over the place. Use the
name
attribute. First of all it does not have to be unique, second it is easy to traverse the elements inside the form.