0

I want to send html table data using ajax to servlet so that I can save it to mysql database, therefore my question is, I prepared the html data as an array and sent it to servlet that is ok, but the problem is while in servlet How can i get each value to save it to database. this is my code.

   <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
   script type="text/javascript">
   $(document).ready(function () { //launch this code after the whole DOM is loaded
    $("form").submit(function (event) { // function to process submitted table
        var a={};
                var tableData = []; // we will store rows' data into this array
                $("#adminTable") // select table by id
                        .find(".tableRow") // select rows by class
                        .has(":checked") // select only rows with checked checkboxes
                        .each(function () { // for each selected row extract data               
                            var tableRow = {};
                            var jRow = $(this);
                            tableRow.customerId = jRow.find('td.customerId').text();
                            tableRow.customerType = jRow.find('td.customerType').text();
                            tableRow.customerKWH = jRow.find('td.customerKWH').text();
                            tableRow.costomerKWD = jRow.find('input.name1').val();
                            tableData.push(tableRow);
                            //alert(tableRow.costomerKWD);

                        });

                $.post(
                        "generateKwd", /*url of consuming servlet*/
                        {tableData: tableData}, /*data*/

                        function () {
                            alert("Success!");
                        }, /*function to execute in case of success*/
                        "json" /* data type */
                );
                event.preventDefault(); //Prevent sending form by browser
            }
    );


});

Abdirazack
  • 43
  • 1
  • 2
  • 11
  • On servlet side, you have to retrieve the value of parameter and using some JSON library you will get all the values – Mohammad Ashfaq Dec 02 '14 at 05:25
  • thanks but how, could you show me please – Abdirazack Dec 02 '14 at 05:27
  • http://stackoverflow.com/questions/19374345/get-parameter-sent-via-jquery-ajax-in-java-servlet – Mohammad Ashfaq Dec 02 '14 at 05:31
  • thanks once more but I tried but did not get yet, let clarify what I want, this above ajax example sends data(tableData) to servlet, but in servlet I know using getParameter to find single request, in this case it is array and I have no idea how to get values in the array, that is what I want. – Abdirazack Dec 02 '14 at 05:43
  • You have to do trial and Error. Try to modify the program given in above question link. Print all the req.getParameterNames and also print the Decoded values. This will give you idea about how to fetch the array – Mohammad Ashfaq Dec 02 '14 at 05:49

0 Answers0