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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

MultipleFields to databse


Go to End


3 Posts   1079 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]