Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » MultipleFields to databse

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

Page: 1
Go to End
Author Topic: 482 Views
  • merrick_sd
    Avatar
    Community Member
    95 Posts

    MultipleFields to databse Link to this post

    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();

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: MultipleFields to databse Link to this post

    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!

  • merrick_sd
    Avatar
    Community Member
    95 Posts

    Re: MultipleFields to databse Link to this post

    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]

    482 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.