Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Creating a new form, saving to table and emailing to site owner

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1619 Views
  • Charly
    Avatar
    Community Member
    6 Posts

    Creating a new form, saving to table and emailing to site owner Link to this post

    I want to create a Contact form that has more than 4 fields in it, save the data into the database and email the result to the siteowner.

    From what I've experienced, the built in Contact Form will only display four fields.

    I have created a new Data Object and Page Type to perform the function:

    CanIUpgradeProcess.php

    class CanIUpgradeProcess extends DataObject {
    static $db = array(
        'EmailFrom' => 'Text',
    'FirstName' => 'Text',
    'Surname' => 'Text',
        'MobileNumUpg'=>'Text',
        'OptusContractNum'=>'Text',
        'PrefContactNum'=>'Text',
        'PrefContactTime1'=>'Text',
        'PrefContactTime2'=>'Text',
        'NewsLetter'=>'Text',
        'Referrer'=>'Text'       
    );
    }

    Upgrade.php

    class Upgrade extends Page {
       static $add_action = "an Upgrade form";
       static $icon = "cms/images/treeicons/task";

    }

    class Upgrade_Controller extends Page_Controller {
       
    function CanIUpgrade() {
    return new Form($this, "CanIUpgrade", new FieldSet(
    // List the your fields here
    new TextField("FirstName", "First Name"),
    new TextField("Surname", "Last Name"),
    new EmailField("EmailFrom", "Email address"),
    new TextField("MobileNumUpg", "Number of Phone To Upgrade"),
    new TextField("OptusContractNum", "Optus Contract # (If known)"),         
    new TextField("PrefContactNum", "Preferred Contact Number"),
    new DropdownField("ContactTime1", "Preferred Contact Time", $source = array(
             "1" => "9am to 12pm",
             "2" => "12pm to 3pm",
             "3" => "3pm to 6pm",
             "4" => "Anytime between 9am and 6pm"),
             $value="4"
             ),
    new DropdownField("ContactTime2", "Alternate Contact Time", $source = array(
             "9am to 12pm" => "9am to 12pm",
             "12pm to 3pm" => "12pm to 3pm",
             "3pm to 6pm" => "3pm to 6pm",
             "Anytime" => "Anytime between 9am and 6pm"),
             $value="9am to 12pm"
             ),
    new DropdownField("Newsletter", "Grab Updates?", $source = array(
             "Yes" => "You Bet! Yes Please!",
             "No" => "Not This Time Thanks"),
             $value="Yes"
             ),
             new HiddenField ("Referrer", "", $_SERVER['HTTP_REFERER'])                                 
    ), new FieldSet(
    // List the action buttons here
    new FormAction("process", "Can I Upgrade?")

    ), new RequiredFields(
    // List the required fields here: "Email", "FirstName"
    ));
    }   
       
       function process( $data, $form ) {
          $submittedForm = new CanIUpgradeProcess();   
          $form->saveInto($submittedForm);
          $submittedForm->write();

          $email = new Email('from@yourdomain.com', 'to@yourdomain.com', 'Can I Upgrade?');
          foreach($data as $key=>$value) {
           $body .= $key.' = '.$value."\r\n";
             //echo $body;
          }
          $email->setBody($body);
          $email->send();
          $url = "home";
        Director::redirect($url);      
       }
    }

    The form displays the way I want it to

    The text fields: Firstname, Surname, EmailFrom, MobileNumUpg, OptusContractNum, Preferred ContactNum, Referrer are all written the to table correctly.

    The dropdownfields and checkboxfieldset are not stored correctly - they are stored as null.

    The form emails, but does not include data, just the field names.

    It appears to redirect.

    Two questions:
    1. How do you write the contents of a submitted dropdownbox and checkboxfieldsets to the data object?

    2. How do you email the results properly?

    This example is based on the recipe found at http://docs.silverstripe.com/?id=recipes:forms

  • Charly
    Avatar
    Community Member
    6 Posts

    Re: Creating a new form, saving to table and emailing to site owner Link to this post

    I have resolved the email issue and the function now returns a very nicely formatted email.

    I am still having problems saving the form data the to DataObject. This what I have so far

    CanIUpgradeProcess.php

    class CanIUpgradeProcess extends DataObject {
    static $db = array(
    'EmailFrom' => 'Text',
    'FirstName' => 'Text',
    'Surname' => 'Text',
    'MobileNumUpg'=>'Text',
    'OptusContractNum'=>'Text',
    'PrefContactNum'=>'Text',
    'PrefContactTime1'=>'Text',
    'PrefContactTime2'=>'Text',
    'NewsLetter'=>'Text',
    'Referrer'=>'Text'
    );
    }

    Upgrade.php

    class Upgrade extends Page {
    static $add_action = "an Upgrade form";
    static $icon = "cms/images/treeicons/task";

    }

    class Upgrade_Controller extends Page_Controller {

    function CanIUpgrade() {
    return new Form($this, "CanIUpgrade", new FieldSet(
    // List the your fields here
    new TextField("FirstName", "First Name"),
    new TextField("Surname", "Last Name"),
    new EmailField("EmailFrom", "Email address"),
    new TextField("MobileNumUpg", "Number of Phone To Upgrade"),
    new TextField("OptusContractNum", "Optus Contract # (If known)"),
    new TextField("PrefContactNum", "Preferred Contact Number"),
    new DropdownField("ContactTime1", "Preferred Contact Time", $source = array(
    "1" => "9am to 12pm",
    "2" => "12pm to 3pm",
    "3" => "3pm to 6pm",
    "4" => "Anytime between 9am and 6pm"),
    $value="4"
    ),
    new DropdownField("ContactTime2", "Alternate Contact Time", $source = array(
    "9am to 12pm" => "9am to 12pm",
    "12pm to 3pm" => "12pm to 3pm",
    "3pm to 6pm" => "3pm to 6pm",
    "Anytime" => "Anytime between 9am and 6pm"),
    $value="9am to 12pm"
    ),
    new CheckboxSetField("Newsletter", "Grab Updates?", $source = array(
    "Yes" => "You Bet! Yes Please!",
    "No" => "Not This Time Thanks"),
    $value="Yes"
    ),
    new HiddenField ("Referrer", "", $_SERVER['HTTP_REFERER'])
    ), new FieldSet(
    // List the action buttons here
    new FormAction("process", "Can I Upgrade?")

    ), new RequiredFields(
    // List the required fields here: "Email", "FirstName"
    ));
    }

       function process( $data, $form ) {
          $submittedForm = new CanIUpgradeProcess();      
          $form->saveInto($submittedForm);
          $submittedForm->write();
          $email = new MyEmail;
          $email->populateTemplate($submittedForm);
          $email->send();
          $url = "thankyou";
        Director::redirect($url);      
       }
    }

    All data except the Dropdownfields save in the DataObject Table. The Dropdownfields save as NULL (they do however, appear correctly in the email).

    Any ideas?

  • Phalkunz
    Avatar
    69 Posts

    Re: Creating a new form, saving to table and emailing to site owner Link to this post

    Hi Charly,

    The field names in the dropdownfields are incorrect. they should be PrefContactTime1 and PrefContactTime2 instead of ContactTime1 and ContactTime2.

    1619 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.