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

$data array empty on form submission


Go to End


1476 Views

Avatar
Shauna G

Community Member, 52 Posts

12 April 2012 at 1:19pm

About two months ago, I upgraded a SilverStripe website from 2.3.5 to 2.4.6. Since the upgrade, some of the forms on my site won't send the data to the submit function (the POST array is empty), including "system" forms like the forgot password form for the admin section and the /admin login form. What I find odd is that I have a nearly identical (but different code instance) form with the same field types, which works just fine, but even copying the fields over doesn't work.

Here's the working form and its submit code:

function ContactUsForm() {
        // Create fields
        $fields = new FieldSet(
                        new HeaderField('Send Us a Message:', '2'),
                        new TextField('NAME', 'Full Name:'),
                        new EmailField('EMAIL', 'E-mail Address:'),
                        new TextField('PHONE', 'Phone:'),
                        new TextField('COMPANYNAME', 'Company Name:'),
                        new TextareaField('MESSAGE', 'Message/Note:'),
                        new TextField('HUMAN', '1+1 =')
        );

        // Create actions
        $actions = new FieldSet(
                        new FormAction('doContactUs', 'Submit')
        );

        return new Form($this, 'ContactUsForm', $fields, $actions);
    }

    function doContactUs($data, $form) {

        $human = $data['HUMAN'];
        if ($human == '2') {
            $from = 'contact@example.com';
            $to = 'contact@example.com';
            $subject = 'General Contact  Submission';
            $body = $data['NAME'] . ' has submitted a General Inquiry, their info: ' . $data['NAME'] . ' | ' . $data['EMAIL'] . ' | ' . $data['PHONE'] . ' | ' . $data['COMPANYNAME'] . ' and they have included the following note (if they included a note): ' . $data['MESSAGE'];
            $email = new Email($from, $to, $subject, $body);
            $email->send();


            Director::redirect('thankyou/');
        } else {
            $form->addErrorMessage('Message', 'Incorrect answer to the human check.','error');
            return Director::redirectBack();
        }
    }

Here's the form that doesn't work, and its submit code:

function GenericContactForm() {

    global $contactmessage;

    // Create fields
    $fields = new FieldSet(
            new TextField('NAME', 'Full Name:'),
            new EmailField('EMAIL', 'Email:'),
            new TextField('TELEPHONE', 'Work Phone:'),
            new ListboxField(
                $name = "TYPEOFCONTACT",
                $title = "Office Type:",
                $source = array(
                "Single Person Office" => "Single Person Office",
                "Team Room" => "Team Room",
                "Open Plan" => "Open Plan"
                ),
                $value = 1
            ),
                    new TextField('HUMAN', '1+1 =')
    );

    // Create actions
    $actions = new FieldSet(
            new FormAction('doGenericContact', 'Submit')
    );

    return new Form($this, 'GenericContactForm', $fields, $actions);
}

function doGenericContact($data, $form) {
    $human = $data['HUMAN'];
    if ($human == '2') {
        $from = $data['EMAIL'];
        $to = 'contact@example.com';
        $subject = 'Contact Request';
        $body = 'The following individual has requested to be contacted: ' . $data['NAME'] . ' | ' . $data['EMAIL'] . ' | ' . $data['TELEPHONE'] . ' and they have made contact to inquire about the following: ' . $data['TYPEOFCONTACT'];
        $email = new Email($from, $to, $subject, $body);
        $email->send();

        Director::redirect('/thankyou/');
    } else {
        $form->addErrorMessage('Message', "Incorrect answer to the human check. (Your answer: $human)", 'error');
        return Director::redirectBack();
    }
}

I've tried removing the ListboxField, since that's about the only real difference I've found. I also tried testing it locally by removing everything inside the "true" portion of the if statement, as well as removing all of the code and simply using var_dump() to dump the $data array, in the submit function, and it works locally, but not on the server. Additionally, these forms worked prior to the upgrade.

Any ideas on what is causing this, and what I can do to fix it?