Rahul Iyer
Rahul Iyer

Reputation: 21025

Why doesn't the select input field in my form get sent to the server?

I have a form:

<form class="form-horizontal" role="form"
      action="/createstudent" method="POST">

    <div class="form-group">
        <label for="fieldName" class="col-sm-2 control-label">First name:</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="fieldName" name="firstname">
        </div>
    </div>
  //....more input fields....

    <div class="form-group">
        <label for="classof">Class of:</label>
        <select class="form-control" id="classof">

        </select>
      </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-4">
            <button type="submit" class="btn btn-default">Create</button>
        </div>
    </div>
</form>
<script>
    $(document).ready(function(){
       var $select = $("#classof");
       var currentTime = new Date();
       var year = currentTime.getFullYear();

       for (var i=0;i<13;i++) {
            $select.append('<option value=' + (year + i) + '>' + (year + i) + '</option>');
        }
    });
</script>

As you can see, the options for the select input field, are populated dynamically, when the page loads. When I post my form (by clicking on the button), the request body contains the details of all the other fields (first name etc.), but there are no details of the select object... Why isn't it in the request body ?

Upvotes: 0

Views: 66

Answers (2)

Samuel James
Samuel James

Reputation: 1536

Name attribute is missing here

<div class="form-group">
        <label for="classof">Class of:</label>
        <select class="form-control" id="classof" name="selected_item">

        </select>
      </div>

Upvotes: 1

Derek Brown
Derek Brown

Reputation: 4419

You need to set the name attribute on your select tag.

Upvotes: 1

Related Questions