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

SOLVED: How can I get a mini enquiry form on everey page?


Go to End


6 Posts   1123 Views

Avatar
LinseyM

Community Member, 99 Posts

28 September 2011 at 7:26am

Hi there,

Not quite sure how to go about this, but a client has asked for a "mini enquiry form" (name, email, message) on the right hand side of every page. How do I do this without having to make all pages into "Form" page types?

I've done a few searches in the forums etc, but can't find what I'm looking for, so hoping someone can point me in the right direction.

Many thanks,

Linsey

Avatar
MarcusDalgren

Community Member, 288 Posts

28 September 2011 at 7:46am

You don't have to make a special page type just to put a form on a page.
Put this or some version of this in your Page_Controller then you can use this form pretty much anywhere on your site.
Just write $EnquiryForm in the template and that should print the form.

    public function EnquiryForm() {
    	$fields = new FieldSet(
    		new TextField("Name", "Name"),
			new EmailField('Email', 'Email'),
			new TextareaField("Message", "Message")
		);
		
        $actions = new FieldSet(
            new FormAction('ProcessEnquiryForm', 'Submit')
        );
         
        return new Form($this, 'EnquiryForm', $fields, $actions);
    }
    
    public function ProcessEnquiryForm($data, $form) {
    	// Process form here...
    }

How you save/process the data is up to you of course.

Avatar
LinseyM

Community Member, 99 Posts

28 September 2011 at 7:55am

Thanks for that.

I've been thinking about it a bit more and I was wondering if there was some way to create a form page (thats hidden from the site), and then call the form itself from this page and show it on any page within the site? (That way the client could edit the form in the CMS if they wanted to).

Cheers, L x

Avatar
MarcusDalgren

Community Member, 288 Posts

28 September 2011 at 8:26am

Are you talking about the UserForms module?

In that case you might be able to create a page on the site that isn't visible in menus and then fetch that with a function.
Something like

function UserDefinedForm() {
  $form = DataObject::get_by_id("UserDefinedForm", $yourId);
  $controller = new UserDefinedForm_Controller($form);
  return $controller->Form();
}

Haven't tried this myself though so I'm not sure if the form will work on another page like this.

Avatar
LinseyM

Community Member, 99 Posts

28 September 2011 at 8:34am

OK, that would make sense... I will give it a try and see how I get on.

Cheers! :-)

Avatar
LinseyM

Community Member, 99 Posts

14 October 2011 at 11:57pm

Just to say I found another way to do this: http://www.silverstripe.org/all-other-modules/show/8636