发送html表数据到mysql表中 [英] sending html table data into mysql table

查看:106
本文介绍了发送html表数据到mysql表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form action =index.php这是一个动态的(通过jquery)html表格,方法= POST > 
< table class =myTableid =cup>
< tbody>
< tr>< td class =headercolspan =6>您的选择< / td>< / tr> < td>< td>< / td>< td>< / td>< td>< / td> / TD>< TD>< / TD>< / TR> //选择
< / tbody>
< / table>
< input type =submitvalue =submit>
< / form>

首先,表格只有1行和6个单元格(选择部分除外)。满足一些条件后,单元格会填充一些值,选定行通过jquery添加到表中。但我的问题在这里,与jquery无关。



6个单元格中的每一个都将被逐一插入到mysql表格中。我的数据库表结构像这样:

  ID |选择1 | ... | Selection6 |日期,时间和其他信息。 

所以,我需要一些代码来完成这些工作:

 定义一个包含6个元素的数组。 
counter = 0;
循环(直到html表格中没有行进的单元格)
{
找到一个不为NULL的单元格,或者它的值不是选择;
array [counter]< - cell的值;
counter ++;
if(counter == 6)
{
将它们全部插入mysql表(?)
counter = 0;
}
} // end loop

所以,这是我的问题。我只是不知道该怎么做。我应该在html标签中添加id还是name?或者什么? :)



编辑:我不希望你们为我编码当然。我无法得到的东西是如何将这些值分开6并将它们发送到数据库中。我只需要点子。

解决方案

从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,...} ....]
//现在您可以使用一个键将所有数据发送到您的服务器
$ .post(url,{mydata:JSON.stringify(myData)},function(d){
alert('Server说,'+ d);
});
//在您的PHP脚本中,您将查找mydata中的数据 - > $ _POST ['mydata']

编辑



点击下面的链接进行演示。



THIS DEMO 点击提交,然后查看右侧的控制台。点击失败的ajax调用左边的加号,然后点击Post选项卡检查调用尝试发送的数据。



这有帮助吗?


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.

解决方案

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?

这篇关于发送html表数据到mysql表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆