This is tabular input and must be treated as such.
You have your input names like: teammemeber[fname] when then should be teammemeber[0][fname],teammemeber[1][fname] etc.
Now from what you described, you clone the original input fields, but you’ll have to handle them with jQuery something like:
// this gets triggered when + is pressed
function insertFieldsInHtmlForm(){
var uniqueID = (new Date()).getTime();
var fields = '<input type="text" name="teammemeber['+uniqueID+'][fname]" />';
fields+= '<input type="text" name="teammemeber['+uniqueID+'][lname]" />';
fields+= '<input type="text" name="teammemeber['+uniqueID+'][email]" />';
$("form#your-form-id").append(fields);
}
The above code will assure that you insert tabular input fields and when you save them, you will have an array like:
Array(
123456789 => ( fname=>some text, lname=>other text, email=> email@address ),
123456799 => ( fname=>new text, lname=>new again, email=>email@new)
);
Now when this form is sent you will have to loop through $_POST[teammmember], like:
if(isset($_POST['teammember'])){
foreach($_POST['teammeber'] AS $uniqueID=>$attributesArray){
$member=new TeamMember();
$member->attributes=$attributesArray;
$member->save();
}
}
This will get you going with "creating" part, when updating, it can be done like:
if(isset($_POST['teammember'])){
foreach($_POST['teammeber'] AS $uniqueID=>$attributesArray){
$member=TeamMember::model()->findByPk((int)$uniqueID);
if(empty($member))
$member=new TeamMember();
$member->attributes=$attributesArray;
$member->save();
}
}
Mainly because, when you update records, and you generate the input fields you will do it like:
if(!empty($members)){
foreach($members AS $member){
echo '<input type="text" name="teammember['.$member->member_id.'][fname]" />';
echo '<input type="text" name="teammember['.$member->member_id.'][lname]" />';
echo '<input type="text" name="teammember['.$member->member_id.'][email]" />';
}
}
As you see, when updating records, we use the member ID to group the tabular input and when we create new records, we use a unique integer generated from javascript, namely the current time in microseconds, this assures that there will be no collision between existing fields and the new created fields.
I think i explained in details all you have to do 