Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

MultipleFields to databse


Go to End


3 Posts   805 Views

Avatar
merrick_sd

Community Member, 99 Posts

18 August 2012 at 2:46am

I have an online form where the user is able add multiple delegates

eg: name,email ..etc
and you click add to add another input box

so for example the form would end up with this on submission for 4 delegates
<!-- delegate One -->
< input type="text" class="text rounded" id="Form_EventBookingForm_DelFirstName" name="DelFirstName" value="" />
< input type="text" class="text rounded" id="Form_EventBookingForm_DelLasttName" name="DelLastName" value="" />

<!-- delegate Two -->
< input type="text" class="text rounded" id="Form_EventBookingForm_DelFirstName_0" name="DelFirstName_0" value="" />
< input type="text" class="text rounded" id="Form_EventBookingForm_DelLasttName_0" name="DelLastName_0" value="" />

<!-- delegate Three -->
< input type="text" class="text rounded" id="Form_EventBookingForm_DelFirstName_1" name="DelFirstName_1" value="" />
< input type="text" class="text rounded" id="Form_EventBookingForm_DelLasttName_1" name="DelLastName_1" value="" />

<!-- delegate Four -->
< input type="text" class="text rounded" id="Form_EventBookingForm_DelFirstName_2" name="DelFirstName_2" value="" />
< input type="text" class="text rounded" id="Form_EventBookingForm_DelLasttName_2" name="DelLastName_2" value="" />

How can i collect this and save for each () one to the database.
I know how to get i entry saved.

//save the Delegate(s)
$delegates = New Delegates();
$fields = array(
'UniqueOrderID',
'DelegateTitle',
'DelFirstName',
'DelLastName',
'DelJobTitle',
'DelEmail',
'Dietary',
);
$form->saveInto($delegates, $fields);
$delegates->write();

Avatar
swaiba

Forum Moderator, 1899 Posts

18 August 2012 at 4:02am

I do this and I use the same logic to create the form as to save it...

1. when building your form there is a loop right? and you use that to add the "_X" for the delegates?

e.g.

for($i=0; $i<$iNumDelegates;$i++) {
	$fields->push(new TextField('DelFirstName_'.$i))
	//repeat for each field
	//I use another loop on the getFrontEndFields method of the required object
}

2. when you save go through the same loop like you are making the fields and then instead save them into a new object

for($i=0; $i<$iNumDelegates;$i++) {
	$delegate = new Delegate();
	$delegate->setField('DelFirstName','DelFirstName_'.$i)
	//repeat for each field
	$delegate->write();
}

Hope this helps!

Avatar
merrick_sd

Community Member, 99 Posts

22 August 2012 at 3:11am

Yes this really helped me
many thanks

	$NumDelegates = $data['DelegateCount'];
				$iNumDelegates = ($data['DelegateCount'] - 1);
		if ($NumDelegates > 1) {
		
	
		$iNumDelegates = ($data['DelegateCount'] - 1);
			for($i=0; $i<$iNumDelegates;$i++) {
			   $delegate = new Delegates();
			    $delegate->setField('BookingEventID',$bookingeventid->ID);
			    $delegate->setField('BookeeUserID',$bookeeuser->ID);
			   $delegate->setField('DelFirstName',$data['DelFirstName_'.$i]);
			   $delegate->setField('DelLastName',$data['DelLastName_'.$i]);
			   $delegate->setField('DelEmail',$data['DelEmail_'.$i]);
			   $delegate->setField('DelJobTitle',$data['DelJobTitle_'.$i]);
			   $delegate->setField('DelDietary',$data['DelDietary_'.$i]);
			   //repeat for each field
			   $delegate->write();
			}
		}
[\code]