Skip to content Skip to sidebar Skip to footer

Jquery Ajax Post To Mysql Table For Dynamic Table Data- Am I Doing This In Anything Like The Right Way?

I have a table which I can dynamically add and delete any number of rows to. Once the data is all entered by the user I am using the jQuery AJAX functionality to POST it to a mysql

Solution 1:

It looks very clearly to me as if you have a well-established index of the row in question, using your variable i. Most form handlers server-side will unpack repeated keys of the form into a list, for stuff like many checkboxes with the same name. You could exploit that here.

datastring = '';
for(var i=2; i<rowCount; i++) {
    var txtRow1 = jQuery('#txtRow' + (i-1)).val();
    var tickerRow1 = jQuery('#tickerRow' + (i-1)).val();
    var quantRow1 = jQuery('#quantRow' + (i-1)).val();
    var valueRow1 = jQuery('#valueRow' + (i-1)).val();
    dataString = datastring + 'index[]=' + (i-1) + 'txtRow1[]='+ txtRow1 + '&tickerRow1[]=' + tickerRow1 + '&quantRow1[]=' + quantRow1 + '&valueRow1[]=' + valueRow1;
}

Then make the ajax call with the whole string.

On the server-side, you should get arrays for each of these, the first of each of which corresponds to the first row, the second of each of which corresponds to the second row, and so on.

It's been a long time since I used PHP, but I believe the [] symbols for each key item are necessary to clue PHP's $_POST that it should convert the various keys' contents into arrays.

Solution 2:

I would try posting it all at once.

Reasons

  1. fewer calls, less chance of your message getting lost in transit (your server rejecting requests because of flood)
  2. you don't have to worry about requests responding out of order (maybe not an issue, but having an ass load of jumbled responses could potentially be an issue)
  3. it will be faster for large numbers of rows getting saved at once

Post a Comment for "Jquery Ajax Post To Mysql Table For Dynamic Table Data- Am I Doing This In Anything Like The Right Way?"