php / jquery:发布表单数据

时间:2012-11-21 12:23:30

标签: php jquery forms formatting

我写了一个用于编辑数据库数据的网格小部件,我正在寻找一种更方便的方式将数据发送到我的保存脚本。

表单包含文本框和以下数据库结构:

recordID field         value      field           value
----------------------------------------------------------
1        first_name    andy        last_name      smith
2        first_name    tom         last_name      jones
3        first_name    john        last_name      connor

目前我正在将recordID添加到fieldname,以这种方式发送:

first_name/1=andy&last_name/1=smith&first_name/2=tom.....

问题是在服务器脚本上我需要解析recordID以进行适当的保存。 有一种更方便的方法,我可以用recordID包装每条记录吗?

such as: record1 = firstname:andy, last_name:smith | record2 = ... ?

我正在用jQuery构建post字符串。 谢谢!

2 个答案:

答案 0 :(得分:1)

尝试命名您的字段:

records[1][first_name]=andy&records[1][last_name]=smith&records[2][first_name]=tom.....

答案 1 :(得分:1)

将您的数据形成一个数组(此示例中为arrayOfData),然后您可以使用jQuery ajax调用将其作为字符串发送到php(本例中为phppage.php)。类似下面的东西会起作用。要使用它,您需要来自herestringify函数的JSON.js。

$.post("phppage.php",{
    fieldName:JSON.stringify(arrayOfData)
},function(return){
   //whatever
});

这需要在php中解析

$incArray = json_decode(fieldName, true);

只是一个例子,并非所有工作都在这里完成。