'Short cut way of collecting form input values using javascript

I have a form that collects information from a user. The form is composed of 10 input text field. The individual value of input text field is accessed via var first_name = $("#fname").val() as an example and passed to var postdata = {'fname':first_name}; and then passed to ajax with url: "<?php echo base_url(); ?>main_controller/save_data", data: postdata then the controller function again will collect it. If I have 30 input text field and then used this way, it would take me a lot of time. What is the best way to pass numerous input field values using javascript in miniature way? Is there a shortcut for this? Thanks a lot. I have a sample code below.

Views:

<input class="input input-large" type="text" name="fname" id="fname" value=""/>
<input class="input input-large" type="text" name="lname" id="lname" value=""/>
<input class="input input-large" type="text" name="address" id="address" value=""/>
<input class="input input-large" type="text" name="age" id="age" value=""/>
<input class="input input-large" type="text" name="height" id="height" value=""/>
<input class="input input-large" type="text" name="gender" id="gender" value=""/>
<input class="input input-large" type="text" name="school" id="school" value=""/>
<input class="input input-large" type="text" name="course" id="course" value=""/>
<input class="input input-large" type="text" name="year" id="year" value=""/>
<input class="input input-large" type="text" name="date_of_birth" id="date_of_birth"
value=""/>

<button class="btn btn-success" id="save" name="save">Save</button>

<script type="text/javascript">
$("#save").click(function(){
    var fname = $("#fname").val();
    var lname = $("#lname").val();
    var address = $("#address").val();
    var age = $("#age").val();
    var height = $("#height").val();
    var gender = $("#gender").val();
    var school = $("#school").val();
    var course = $("#course").val();
    var year = $("#year").val();
    var date_of_birth = $("#date_of_birth").val();

    var postdata = {
            'fname': fname,
            'lname': lname,
            'address': address,
            'age': age,
            'height': height,
            'gender': gender,
            'school': school,
            'course': course,
            'year': year,
            'date_of_birth': date_of_birth
    };

    $.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>main_controller/save_data",
        data: postdata,
        success: function (data) {
            if (data.notify) {
                alert('success');
            } else {
                alert('Failed');
            }
        }
    });

    });
</script>

Controller:

function save_data(){
    $fname = $this->input->post('fname');
    $lname = $this->input->post('lname');
    $address = $this->input->post('address');
    $age = $this->input->post('age');
    $height = $this->input->post('height');
    $gender = $this->input->post('gender');
    $school = $this->input->post('school');
    $course = $this->input->post('course');
    $year = $this->input->post('year');
    $date_of_birth = $this->input->post('date_of_birth');
    $this->insert_model->save_data($fname,$lname,$address,$age,$height,$gender,$school,$course,$year,$date_of_birth);
 }


Solution 1:[1]

You could do this:

var inputs, index;

inputs = document.getElementsByTagName('input');
for (index = 0; index < inputs.length; ++index) {
// get inputs[index] element.
}

You can get more information from the NodeList reference.

Solution 2:[2]

Use JQuery serialize() method It effectively turns the form values(input/select..) into a valid querystring.

$.ajax({
   type:"POST",
   url:"<?php echo base_url(); ?>main_controller/save_data",
   data:$('form').serialize(),
   success: function(data){
   if(data.notify){
   alert('success');
  }
  else{
   alert('Failed');
  }
 }
});

serialize() documentation:link

Solution 3:[3]

var formelements = {}
$(input).each()
{
formelements[$(this).attr('name')] = $(this).val();
}

now all your values are stored inside the formelements, you can even send this element instead of manually sending all form elements:

$.ajax({
   type:"POST",
   url:"<?php echo base_url(); ?>main_controller/save_data",
   data:formelements,
   dataType:"json",
   success: function(data){
   if(data.notify=="Success"){
   alert('success');
  }
  else{
   alert('Failed');
  }
 }
});

Solution 4:[4]

Non-serialization alternative:

If you need an array of form fields somewhere else in your script (for example, for client-side validation purposes) you can use the following alternative of .serialize():

var postdata = {};
$(".input").each(function () {
  postdata[$(this).attr("name")] = $(this).val();
});

Now postdata contains the array of values (like in your initial code snippet) instead of querystring.

P.S.: Add some special class attribute to your form fields instead of input which I have used just for example.

Solution 5:[5]

var fname=fname.value;
var lname=lname.value;
var address=address.value;
var age=age.value;
var height=height.value;
var date_of_birth=date_of_birth.value;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 geekchic
Solution 2 harrybvp
Solution 3 Vincent Duprez
Solution 4 zavg
Solution 5 Rameshwar Kadam