2

I am trying to get all the values present in my dynamic HTML table and POST the values to AJAX.

I have HTML table like this

enter image description here

When i press '+' , I can able to add rows dynamically . When i click save, How to POST array values from this HTML table to AJAX, so that i can meanwhile INSERT those values into MYSQL

I have tried getting the 'text' of each td present in my table

var rows = $("tbody tr",$("#myTable")).map(function() { 
    return [$("td",this).map(function() { 
      return this.innerHTML;     
    }).get()];
  }).get();

This is getting me -> <input type="text"> and so on.

1
  • 1
    Try using jQuery .serialize() on the form to create JSON which can be sent in POST Commented Dec 10, 2014 at 9:25

3 Answers 3

3

you are returning the innerHTML, which is... HTML. you can get the value of a input element with .val() how does this work for you?

return $("td input").map(function() { 
  return $(this).val();     
});
Sign up to request clarification or add additional context in comments.

Comments

3

Use serialize function using jQuery

<script type="text/javascript">
    (function ($) {
        $('#formID').on('submit', function (e) {
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: '/pathTo/process_form.php',
                data: $('#formID').serialize()
            });
        });
    })(jQuery);
</script>

Comments

1
var rows = $("td input",$("#myTable")).map(function() { 
    return { name : $(this).attr('name'), value :$(this).val()};
  });

and you should get :

[ 
  { 
    name : 'inputname',
    value :  'inputval' 
  },
  { ... }
]

This would return an array like the .serialize() method

1 Comment

i am getting this error Uncaught TypeError: Illegal invocation - jquery 1.6.1 min.js

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.