我创建了一个多步表单,我可以在其中编写表单名称,在表单中添加步骤和输入类型,然后生成一个数组或对象(我不确定哪个一个在我的情况下将是最好的),通过这些步骤循环并获取每个输入类型的信息等。最后,通过Ajax将该对象或数组发送到PHP。创建对象或数组来处理多步表单
现在,我正在尝试生成数组或对象,因为我不确定哪一个是最好的,也不知道如何生成它。
我想我需要的,应该是:
var form = {
idForm: "form-" + Date.now(),
name: whatever you want, //get from the form
steps: {
0: {
idStep: "step-" + Date.now(),
name: get from form,
fields: {
0: {
idField: "field-" + Date.now(),
label: field 1,
placeholder: Enter data,
name: field 1,
required: true,
type: text,
options: {...} // this one should be added if type is a select, radio or checkbox.
},
1: {...},
2{...}
}
},
1: {...},
2: {...}
}
}
如何产生的呢任何暗示?任何提示是可喜的。
编辑: 一些研究,我终于可以生成我上面的对象后,我不得不做出一个正常的循环,通过步骤获得我需要的每个信息(使用jQuery。每()) 。所以要填充对象我做了这样的事情:
formObj = {} // create an object
//create the node 'form' in the object and populate with id and name.
formObj.form = { id: "form-"+Date.now(), name: formName.val() };
//then create an array of steps inside the form node
formObj["form"]["steps"] = [];
等等,这样我生成的对象。
当我做在PHP的print_r检查:
Array
(
[form] => Array
(
[id] => form-1497888655149
[name] => formulario 1
[steps] => Array
(
[0] => Array
(
[name] => step 1
[idStep] => step-1497886778424
[fields] => Array
(
[0] => Array
(
[idField] => input-1497886779390
[label] => txéxt1
[name] => null
[required] => true
[type] => text
[options] => null
)
)
)
)
)
)
*无论你想要*会抛出语法错误。至少提供一个有效的表格树... –
@Jonasw你的意思是?因为我忘记了逗号? –
周围有很多表单生成器脚本...检查它们如何工作 – charlietfl