I am creating a data entry form for a school. The school will be entering data in this database for their 5th till 12th grade students. Now 5th grade has 4 sections and all other grade has 3 sections. 10th grade has only one section.
The form is ready and using jquery i am able to make the section select element either show or hide depending on the grade the user selects. My problem is when the user selects std as 5th the section select element is shown and the user selects say “A” and submits. But the value i get is 0. If the user selects section as “D” , the value being sent is “B”.
The section select for all other grades are working fine, this problem arises only when the grade selected is 5th.
Script
<script type='text/javascript' src='scripts/jquery-1.5.2.js'></script>
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
$("#std").change(function() {
var value = $(this).val();
if(parseInt(value)==0) {
$("#sec5").hide();
$("#sec6").hide();
}
if(parseInt(value)==5) {
$("#sec5").show();
$("#sec6").hide();
}
if(parseInt(value)==6) {
$("#sec5").hide();
$("#sec6").show();
}
if(parseInt(value)==7) {
$("#sec5").hide();
$("#sec6").show();
}
if(parseInt(value)==6) {
$("#sec5").hide();
$("#sec6").show();
}
if(parseInt(value)==8) {
$("#sec5").hide();
$("#sec6").show();
}
if(parseInt(value)==9) {
$("#sec5").hide();
$("#sec6").show();
}
if(parseInt(value)==10) {
$("#sec5").hide();
$("#sec6").hide();
}
if(parseInt(value)==11) {
$("#sec5").hide();
$("#sec6").show();
}
if(parseInt(value)==12) {
$("#sec5").hide();
$("#sec6").show();
}
});
});//]]>
</script>
HTML Part
<form action="check.php" method="post">
<fieldset>
<legend>DBMS</legend>
<label for="roll">Roll:
<input name="roll" type="text" id="roll" value="" size="8" maxlength="10" />
</label>
<label for="marks">Total Marks:
<input name="marks" type="text" id="marks" value="" size="3" maxlength="3" />
</label>
<label for="std">Std</label>
<select id="std" name="std">
<option value="0">--Select--</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<span id="sec5" class="hide">
<label for="sec">Section</label>
<select id="sec" name="sec">
<option value="0">--Select--</option>
<option value="A">Sec A</option>
<option value="B">Sec B</option>
<option value="C">Sec C</option>
<option value="D">Sec D</option>
</select>
</span> <span id="sec6" class="hide">
<label for="sec">Section</label>
<select id="sec" name="sec">
<option value="0">--Select--</option>
<option value="A">Sec A</option>
<option value="B">Sec B</option>
<option value="C">Sec C</option>
</select>
</span>
<p align="center">
<input type="submit" name="nlogin" id="nlogin" value="Submit" />
<input type="reset" name="nreset" id="nreset" value="Reset" />
</p>
</fieldset>
</form>
When you
show()orhide()one area of the form, it isn’t removed — just hidden from sight. So your form is actually submitting two select dropdowns with the name (and ID — a big no-no) ofsec. Your problem is caused by the second select’s value overwriting the first.It’s possible to solve this by
remove()ing one of the dropdowns, but that wouldn’t be reversible and I wouldn’t recommend it.Instead, you might change the name/ID of at least one of those select dropdowns — say,
sec1andsec2— and add some server-side code to see which one should be processed.A second solution might be to just have one select in your HTML and use jQuery to modify its options. This will require rewriting most of your JavaScript code, but it will eliminate the need for server-side coding. (However, server-side validation is ALWAYS a good idea in these cases, as a redundancy, and should be implemented anyway.) Something like:
JS: