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

How do I pass checkbox data in an email form?


Go to End


2 Posts   1876 Views

Avatar
robskinn

Community Member, 12 Posts

31 January 2015 at 9:10am

Edited: 02/02/2015 11:09am

Hi,
I'm trying to get the value of a checkbox to send in an email form but I can't figure out the logic behind it. Can someone help please.

For the textfields I;m using (<p><strong>Name:</strong> {$data['Name']}</p> ) to display the data but the checkbox looks like it needs a value to be pre-populated beforehand i.e if I set the values to true it works, but it also ticks the checkbox by default.

The error i'm getting once I submit my form is; [Notice] Undefined index: error.

This is the code that I'm using;

public function Form() { 
        $fields = new FieldList( 
            new CheckboxField('Teacher'),
            new TextField('Name'), 
            new EmailField('Email'), 
            new TextareaField('Message')
            );        
        
        $actions = new FieldList( 
            new FormAction('submit', 'Submit') 
        ); 
    
         $validator = new RequiredFields('Email');
            return new Form($this, 'Form', $fields, $actions, $validator); 
    }
    public function submit($data, $form) { 
        $email = new Email(); 

        $email->setTo(''); 
        $email->setFrom($data['Email']); 
        $email->setSubject("Contact Message from {$data["Name"]}"); 

        $messageBody = " 
            <p><strong>Name:</strong> {$data['Name']}</p> 
            <p><strong>Email:</strong> {$data['Email']}</p> 
            <p><strong>Message:</strong> {$data['Message']}</p>            
            <p><strong>Teacher:</strong> {$data['Teacher']}</p>
        "; 
        $email->setBody($messageBody); 
        $email->send(); 
        return array(
            'Content' => '<h1>Thank you for getting in touch.</h1>',
            'Form' => '<h1>Thank you for getting in touch.</h1>'
        );
    }  

I couldn't get this working?

static $defaults = array (
    'MyBoolean' => '0'
);

Avatar
JonShutt

Community Member, 244 Posts

5 February 2015 at 2:59am

The checkbox field will pass a '1' / 'true' if it's ticked - and nothing if it's not ticked.

Generally it's not too pretty to have the emails send "Teacher: 1".
Better to change it to a "yes" / "no"

$teacher = isset($data['Teacher'])  ? 'yes' : 'no';

        $messageBody = " 
            <p><strong>Name:</strong> {$data['Name']}</p> 
            <p><strong>Email:</strong> {$data['Email']}</p> 
            <p><strong>Message:</strong> {$data['Message']}</p>            
            <p><strong>Teacher:</strong> {$teacher}</p>
        ";