[英]dynamic name property for php
我使用此代码在HTML页面中基于索引键为用户使用加号按钮添加的每个输入标签生成动态名称属性:
var afterRemove = function afterRemove() {
var setIndex = function setIndex(inputNamePrefix) {
$('input[name^="' + inputNamePrefix + '"]').each(function (index, value) {
$(this).attr('name', '' + inputNamePrefix + index);
});
};
['jobTitle', 'organName', 'jobYearFrom', 'jobYearTo'].forEach(function (prefix) {
setIndex(prefix);
});
['research', 'researchComment'].forEach(function (prefix) {
setIndex(prefix);
});
['teachingsub', 'teachingpr', 'teachingplace'].forEach(function (prefix) {
setIndex(prefix);
});
['teacherMobile', 'teacherTel', 'teacherEmail'].forEach(function (prefix) {
setIndex(prefix);
});
['fieldofstudy', 'univercity', 'eduYearFrom', 'eduYearTo', 'eduPaper'].forEach(function (prefix) {
setIndex(prefix);
});
};
现在,我需要在PHP中动态获取每个名称属性。 例如,用户可能有3个phoneNumber: {teacherMobile0,teacherMobile1,teacherMobile2}
。 对于每个主题,我都需要一个PHP变量。 我怎么知道用户生成了多少个名字?
好的,我假设您有一个输入表,并且您让用户通过单击按钮运行该脚本来添加新行,从而向该表中添加行
如果您将字段命名为
<td><input name="fieldofstudy[]" .....></td>
<td><input name="univercity[]" .....></td>
etc etc
您不再需要担心在这些输入中添加前缀或后缀计数。
然后,您需要在javascript中创建新行的所有操作是克隆表中的最后一行,将每个单元格的.val()
空白, .val()
准备开始新行
然后在PHP中,您将收到每个变量的数组,即
$_POST['fieldofstudy'][]
$_POST['univercity'][]
您可以处理为
foreach ( $_POST['fieldofstudy'] as $idx => $fieldofstudy) {
echo $fieldofstudy;
echo $_POST['univercity'][$idx];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.