1

here is my html table code which contains php foreach loop for fillig the html table data, i want to post this whole table data to my controller as a array by form.serialize in ajax, can i do this??how??

<table class="table table-striped table-vcenter table-bordered">
                        <thead>
                        <th>Master Name</th>
                        <th>From No.</th>
                        <th>To No.</th>
                        <th>Total</th>
                        </thead>
                        <tbody>
                            <?php foreach ($master as $name) { ?>
                                <tr class="rc1_tr">
                                    <td class="td_amount"><div id="amount"><?php echo $name['amount']; ?></div></td>
                                    <td id="form_no"><div id="div_to_no"><?php echo $name['receipt_no_to'] ? $name['receipt_no_to'] : '0'; ?></div></td>
                                    <td align='center' class="qt" id="<?php echo $name['id'] ?>" style="width: 250px;"><input type="text" id="to_no" class="quantity" value="" style="text-align:center;width:180px;height:26px;margin-bottom: 2px;"/></td>
                                    <td id="td_total"><div id="total"></div></td>
                                </tr>
                            <?php } ?>
                        </tbody>
                        <tfoot>
                            <tr class="grand_total">
                                <td colspan="3">Grand Total</td>
                                <td><div id="div_grand_total"></div></td>
                            </tr>
                        </tfoot>
                    </table>
2

2 Answers 2

0

You can serialize all inputs, try this

//dynamic query_string generation
var filters_query_string = "&" + $(".rc1_tr input").map(function (i,e) {
    return $(e).serialize()
    }).toArray().join("&")

$.ajax({url  : $("#your_form").attr('action') +filters_query_string, 
        //other ajax params
        success: function(data) {
            //do stuff
        },
        dataType: 'json' /* return type, f.e: json  */,
        error: function(error) {
           console.log(error);                   
        },
})        
Sign up to request clarification or add additional context in comments.

1 Comment

i don't have inputs inside table, it contains only simple text.
0

try in this way.. put its content inside javascript object

var rowIndex=0;
var columnIndex=0;
var tableJson = [];
$(".table tr").each(function () {
  var row = $(this);
  $("td", row).each(function () {
    var tdElem= $(this);
    tableJson.push({row=rowIndex,column=columnIndex,value=tdElem.html()});
    columnIndex+=1;
  });
 rowIndex+=1;
});

put this code inside a function

function getJsonTable(){
   //previous code
  return JSON.stringify(tableJson);
}

and pass the result as data parameter of jquery ajax call

3 Comments

it give tableJson is undefined and can't set property 0
have you put this var tableJson = new Object();?
yes,but you have to add,tableJson[rowIndex] = new Object();,and it works

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.