javascript - dynamic name property for php -
i use code generating dynamic name attribute in html page based on index key each input tag user add 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 need each name attribute dynamically in php. example, user may have 3 phonenumber: {teachermobile0,teachermobile1,teachermobile2}
. each theme need variable in php. how can know how many name has been generated user?
ok assume have table of inputs , let user add rows table click on button runs bit of javascript add new row
if name fields
<td><input name="fieldofstudy[]" .....></td> <td><input name="univercity[]" .....></td> etc etc
you no longer have worry adding prefix or suffix counts these input.
then need create new row in javascript clone last row in table, blank .val()
of each cell , new row ready go
then in php receive array each variable i.e.
$_post['fieldofstudy'][] $_post['univercity'][]
which can process
foreach ( $_post['fieldofstudy'] $idx => $fieldofstudy) { echo $fieldofstudy; echo $_post['univercity'][$idx]; }
Comments
Post a Comment