Php 将html表数据发送到mysql表

Php 将html表数据发送到mysql表,php,jquery,html,mysql,html-table,Php,Jquery,Html,Mysql,Html Table,我有一个动态(通过jquery)html表,如下所示: <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&

我有一个动态(通过jquery)html表,如下所示:

<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>
ID |  Selection1   |...|  Selection6  |  Date, time stuffs and other info.
因此,我需要一些代码来完成这些工作:

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
定义一个包含6个元素的数组。
计数器=0;
循环(直到html表中没有移动的单元格)
{
查找不为NULL或其值不是“selections”的单元格;

数组[counter]从html表数据可以构造一个JSON对象。例如:

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']
编辑

单击下面的链接转到演示

在中,单击submit,然后查看右侧的控制台。单击失败的ajax调用左侧的加号,然后单击Post选项卡检查调用试图发送的数据


这有帮助吗?

从html表数据可以构造JSON对象。例如:

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']
编辑

单击下面的链接转到演示

在中,单击submit,然后查看右侧的控制台。单击失败的ajax调用左侧的加号,然后单击Post选项卡检查调用试图发送的数据


这有帮助吗?

从html表数据可以构造JSON对象。例如:

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']
编辑

单击下面的链接转到演示

在中,单击submit,然后查看右侧的控制台。单击失败的ajax调用左侧的加号,然后单击Post选项卡检查调用试图发送的数据


这有帮助吗?

从html表数据可以构造JSON对象。例如:

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']
编辑

单击下面的链接转到演示

在中,单击submit,然后查看右侧的控制台。单击失败的ajax调用左侧的加号,然后单击Post选项卡检查调用试图发送的数据



这有帮助吗?

@JayBlanchard当然没有。我只是不知道如何将所有值6×6分开并发送到数据库中。假设有6行,这意味着我必须使用mysql命令6次。您可以创建一个循环,遍历每行,然后提交该行的值。您是否尝试发送JSON字符串,contai我想@JayBlanchard就是你要怎么做的。在SQL 2008和更高版本中,你可以在一次调用中添加多行。OP没有使用SQL 2008@ChaseWalden@JayBlanchard当然不是。我只是不知道如何将所有的值6×6分开,并将它们发送到数据库中。假设是这样是6行,这意味着我必须使用mysql命令6次。您可以创建一个循环,遍历每行,然后提交该行的值。您是否尝试发送一个JSON字符串,包含所有数据,并且可以提交一次?我认为@JayBlanchard是您必须要做的。在SQL 2008和更高版本中,您可以添加多个在一次调用中删除行。OP未使用SQL 2008@ChaseWalden@JayBlanchard当然不是。我只是不知道如何将所有的值6×6分开并发送到数据库中。假设有6行,这意味着我必须使用mysql命令6次。您可以创建一个循环,遍历每行,然后提交该行的值。是吗您是否尝试发送一个JSON字符串,其中包含所有数据,并且可以提交一次?我认为@JayBlanchard是您必须执行的操作。在SQL 2008及更高版本中,您可以在一次调用中添加多行。OP不使用SQL 2008@ChaseWalden@JayBlanchard当然不是。我只是不知道如何将所有的值6×6分开并发送让我们假设有6行,这意味着我必须使用mysql命令6次。你可以创建一个循环,遍历每行,然后提交该行的值。你是否尝试发送一个JSON字符串,包含所有数据,并且可以提交一次?我想@JayBlanchard是你必须要做的。在SQL 2008及更高版本,您可以在一次调用中添加多行。OP未使用SQL 2008@ChaseWalden