I use this code for generating dynamic name attribute in my HTML page based on index key for each input tag that user add with a plus button:
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);
});
};
Now I need to get each name attribute dynamically in PHP. For example, user may have 3 phoneNumber: {teacherMobile0,teacherMobile1,teacherMobile2}
. For each theme I need a variable in PHP. How can I know how many name has been generated by user?
Ok I assume you have a table of inputs and you let the user add rows to that table with a click on a button which runs a bit of javascript to add a new row
If you name the fields as
<td><input name="fieldofstudy[]" .....></td>
<td><input name="univercity[]" .....></td>
etc etc
You no longer have to worry about adding prefix or suffix counts to these input.
Then all you need to do to create a new row in javascript is clone the last row in the table, blank the .val()
of each cell and a new row is ready to go
Then in PHP you will receive an array for each variable ie
$_POST['fieldofstudy'][]
$_POST['univercity'][]
Which you can process as
foreach ( $_POST['fieldofstudy'] as $idx => $fieldofstudy) {
echo $fieldofstudy;
echo $_POST['univercity'][$idx];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.