0

I am devolping a web application using symfony framework. I have aproblem in forms. Here is my code:

$('#bookCleaningForm').submit(function() {
    // get the array of all the inputs 
    var $inputs = $('#bookCleaningForm :input[type=text]');

    // get an associative array of the values
    var values = {};
    var allVAlues='';
    $inputs.each(function() {
        values[this.name] = $(this).val();
            allVAlues = values[this.name];

    });
    alert(allValues);//console.log(allValues);
    saveBoookCleaning(allVAlues);

});

In the loop i got all data in allValues variable.But when I access outside the loop i got only one value.

Please help

5
  • 3
    because you're overriding the previous values, so outside of the loop you'll get only the last value, to get all values outside of loop use object or array like you've used values object. Commented Sep 19, 2015 at 13:37
  • Can You Please Modify my Code. Commented Sep 19, 2015 at 13:39
  • 1
    Hi, what format would you like to store all of the values in? Commented Sep 19, 2015 at 13:40
  • I'll recommend you to use serialize the form data, I guess you want to POST this data to server Commented Sep 19, 2015 at 13:44
  • I want send all values frther in ajax. Commented Sep 19, 2015 at 13:44

1 Answer 1

1

Each time in the each loop you are assigning the variable allValues to the value of the current input. If you want to store the values as an array you could do this:

$('#bookCleaningForm').submit(function() {
  // get the array of all the inputs 
  var $inputs = $('#bookCleaningForm :input[type=text]');

  // get an associative array of the values
  var values = {};
  var allVAlues=[];
  $inputs.each(function() {
    values[this.name] = $(this).val();
        allVAlues.push(values[this.name]);

  });
  alert(allVAlues);//console.log(allValues);
  saveBoookCleaning(allVAlues);

});

Or, if you want them as a string:

$('#bookCleaningForm').submit(function() {
  // get the array of all the inputs 
  var $inputs = $('#bookCleaningForm :input[type=text]');

  // get an associative array of the values
  var values = {};
  var allVAlues='';
  $inputs.each(function() {
    values[this.name] = $(this).val();
        allVAlues += values[this.name];

  });
  alert(allVAlues);//console.log(allValues);
  saveBoookCleaning(allVAlues);

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.