php的动态名称属性

时间:2016-07-11 13:28:05

标签: javascript php html

我使用此代码在我的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中动态获取每个name属性。例如,用户可能有3 phoneNumber: {teacherMobile0,teacherMobile1,teacherMobile2}。对于每个主题,我需要一个PHP变量。我如何知道用户生成了多少名称?

1 个答案:

答案 0 :(得分:1)

好的我假设你有一个输入表,你让用户在该表中添加行,点击一个按钮运行一些javascript来添加新行

如果您将字段命名为

<td><input name="fieldofstudy[]" .....></td>
<td><input name="univercity[]" .....></td>
etc etc

您不必再担心为这些输入添加前缀或后缀计数。

然后你需要做的就是在javascript中创建一个新行是clone表格中的最后一行,空白每个单元格的.val()并且新行准备好了

然后在PHP中,您将收到每个变量的数组,即

$_POST['fieldofstudy'][]
$_POST['univercity'][]

您可以将其作为

进行处理
foreach ( $_POST['fieldofstudy'] as $idx => $fieldofstudy) {
    echo $fieldofstudy;
    echo $_POST['univercity'][$idx];
}
相关问题