My code below has a radio button in the form to select the Sex. But on submitting it's sending as Male=on or Female=on.I want to send only Male/Female to Database. Please tell me the necessary editing I should do. I'm using Bootstrap.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Home - Student Registration Form</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/datepicker.css" rel="stylesheet">
    <style>
      .error{
        color: red;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="row">
        <form id="regform" class="form-horizontal">
          <fieldset>
          <!-- Form Name -->
          <legend>Student Registration</legend>
          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="firstname">First Name</label>
            <div class="col-md-4">
            <input id="firstname" name="firstname" type="text" placeholder="First Name" class="form-control input-md">
            </div>
          </div>
          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="lastname">Last Name</label>
            <div class="col-md-4">
            <input id="lastname" name="lastname" type="text" placeholder="Last Name" class="form-control input-md">
            </div>
          </div>
          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="dob">Date of birth</label>
            <div class="col-md-4">
            <input id="dob" name="dob" type="text" placeholder="Date of birth" class="form-control input-md">
            </div>
          </div>
          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="age">Age</label>
            <div class="col-md-4">
            <input id="age" name="age" type="text" placeholder="Age" class="form-control input-md" disabled>
            </div>
          </div>
          <div class="form-group">
            <label class="col-md-4 control-label" for="sex">Sex</label>
            <div class="col-md-4">
               <label class="radio-inline"><input type="radio" name="Male">Male</label>
                     <label class="radio-inline"><input type="radio" name="Female">Female</label>
            </div>
          </div>
          <!-- Select Basic -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="subjects">Subjects</label>
            <div class="col-md-4">
              <select id="subjects" name="subjects" class="form-control">
                <option value="Database">Database</option>
                <option value="ADA">ADA</option>
                <option value="Networking">Networking</option>
              </select>
            </div>
          </div>
          <!-- Textarea -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="localaddress">Local Address</label>
            <div class="col-md-4">
              <textarea class="form-control" id="localaddress" name="localaddress"></textarea>
            </div>
          </div>
          <!-- Multiple Checkboxes (inline) -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="localaddresscheckbox">Permenant address</label>
            <div class="col-md-4">
              <label class="checkbox-inline" for="localaddresscheckbox">
                <input type="checkbox" name="localaddresscheckbox" id="localaddresscheckbox" value="1">
                Copy Local Address to permanent Address
              </label>
            </div>
          </div>
          <!-- Textarea -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="permenantaddress">Permenant Address</label>
            <div class="col-md-4">
              <textarea class="form-control" id="permenantaddress" name="permenantaddress"></textarea>
            </div>
          </div>
          <!-- Button -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="submit"></label>
            <div class="col-md-4">
              <button id="submit" name="submit" class="btn btn-primary">Submit</button>
            </div>
          </div>
        </fieldset>
        </form>
      </div>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <script src="js/bootstrap-datepicker.js"></script>
    <script src="js/jquery.validate.min.js"></script>
    <script>
      (function() {

        jQuery.validator.addMethod("lettersonly", function(value, element) {
          return this.optional(element) || /^[a-z]+$/i.test(value);
        }, "Letters only please");

        $("#regform").validate({
          rules: {
            dob: "required",
            localaddress: "required",
            permenantaddress: "required",
            firstname: {
                lettersonly: true,
                required: true
            },
            lastname: {
                lettersonly: true,
                required: true
            }
          },
          submitHandler: function(form) {
            form.submit();
          }
        });

        //init datepicker
        $('#dob').datepicker({
            'format': 'yyyy-mm-dd',
            'autoclose': true
        });

        //copy localaddress to permenant address on checkbox click
        $('#localaddresscheckbox').click(function(){
          if($(this).is(':checked')) {
            var localaddress = $('#localaddress').val();
            $('#permenantaddress').val(localaddress); //copy local address to permenant address box
          }
          else {
            $('#permenantaddress').val('');
          }
        });

        //age handler
        $('#dob').datepicker().on('changeDate', function (ev) {
          //get current date
          var today = new Date();
          var currentYear = today.getFullYear(); //current year
          var selectedYear = $(this).val().split('-')[0]; //selected dob year
          var age = Number(currentYear) - Number(selectedYear);
          $('#age').val(age);
        });

      })();
    </script>
  </body>
</html>

Dani AI

Generated

the reason you see Male=on or Female=on is twofold: each radio has a different name, so the browser sends them as separate fields, and you have not set a value, so radios default to sending "on" when checked. Group the radios under one shared name (e.g., sex) and give each input an explicit value. Do not rely on the label text.

Here is a Bootstrap-friendly, accessible pattern that will submit only one key/value pair like sex=M or sex=F:

<div class="form-group">
  <label class="col-md-4 control-label">Sex</label>
  <div class="col-md-4">
    <label class="radio-inline">
      <input type="radio" id="sex-m" name="sex" value="M" required> Male
    </label>
    <label class="radio-inline">
      <input type="radio" id="sex-f" name="sex" value="F"> Female
    </label>
  </div>
</div>

A couple of practical extras:

  • Validation: since you are already using jQuery Validate, make the group required so the user must choose one.
$("#regform").validate({
  // ...your existing rules...
  rules: { sex: { required: true } },
  messages: { sex: "Please choose one." }
});
  • Reading the value in JS (if you need it before submit): var sex = $('input[name="sex"]:checked').val();
  • Server-side: store a normalized value (e.g., M/F or Male/Female) and validate it on the server too. Never trust only client-side checks.
  • UX tip: avoid preselecting one option; let users choose. If you need more options later, you can add another radio with value="O" and label it clearly.

@diafol is spot on about using a single name and explicit values; the tweaks above just add accessibility, validation, and tidy data handling.

Member Avatar for Member #120589
<label class="radio-inline"><input type="radio" name="Male">Male</label>
<label class="radio-inline"><input type="radio" name="Female">Female</label>

Try

<label class="radio-inline"><input type="radio" name="sex" value="Male" checked>Male</label>
<label class="radio-inline"><input type="radio" name="sex" value="Female">Female</label>

You now have sex=Male or sex=Female

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.