0

I have a dynamic (via jquery) html table like this:

<form action="index.php" method="post">
<table class="myTable" id="cup">
<tbody>
<tr><td class="header" colspan="6">YOUR SELECTIONS</td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr> //selected
</tbody>
</table>
<input type="submit" value="submit">
</form>

First, the table has only 1 row and 6 cells (except "selections" part). After some conditions satisfied, the cells get filled with some values and the "selected" row is appended into the table via jquery. But my problem here, is not related to jquery.

Each of 6 cells will be inserted into the mysql table part by part. My database table structure like this:

ID |  Selection1   |...|  Selection6  |  Date, time stuffs and other info.

So, I need some code that will do these jobs:

define an array with 6 element.
counter=0;
loop(until there is no traveled cell in the html table)
{
    find a cell that is not NULL or its value is not "selections";
      array[counter] <-- value of cell;
      counter++;
      if (counter == 6)
      {
         insert all of them into the mysql table (?)
         counter=0;
      }
}//end loop

So, this is my problem. I just can't know how to do it. Should I have to add "id" or "name" stuffs in the html tags or what? :)

EDIT: I don't want you guys to code it for me of course. The thing I couldn't get is how to separate these values 6 by 6 and send them into the database. I just need ideas.

13
  • @JayBlanchard of course not. I just don't know how to seperate all the values 6 by 6 and send them into the database. Lets say there are 6 rows, that means I have to use mysql commands 6 times.
    – Who Cares
    May 20, 2014 at 16:44
  • You could create a loop that goes through each row and then submits the values for that row. May 20, 2014 at 16:47
  • Did your try to send a JSON string, containing all the data and can be submitted once?
    – PeterKA
    May 20, 2014 at 16:48
  • I think that what @JayBlanchard is how you're going to have to do it. In SQL 2008 and later, you can add multiple rows in one call. stackoverflow.com/questions/452859/… May 20, 2014 at 16:51
  • The OP isn't using SQL 2008 @ChaseWalden May 20, 2014 at 16:53

1 Answer 1

3

From the html table data you can construct a JSON object. For instance:

var myData = [],
    keys = ['ID','Selection1','Selection2', ..... ]
    url = './index.php';
$('table').find('tr:gt(0)').each(function( i, row ) {
   var oRow = {};
   $( row ).find( 'td' ).each( function( j, cell ) {
      oRow[ keys[ j ] ] = $( cell ).text();
   });
   myData.push( oRow );
});
//myData = [ {"ID":"3u3y","Selection1",....},{"ID":"jdjdj",...}.... ]
//now you can use one key to send all the data to your server
$.post( url, { mydata: JSON.stringify( myData ) }, function(d) { 
   alert( 'Server said, ' + d ); 
});
//In your PHP script you would look for the data in mydata --> $_POST['mydata']

EDIT

Click the link below to go to a demo.

In THIS DEMO click submit and then look at the console on the right. Click the plus on the left of the failed ajax call and then click the Post tab to examine the data the call attempted to send.

Does that help?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.