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.

Form Questions

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


Reply

6 Posts   547 Views

Avatar
LinseyM

28 September 2011 at 7:26am Community Member, 99 Posts

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

28 September 2011 at 7:46am Community Member, 288 Posts

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

28 September 2011 at 7:55am Community Member, 99 Posts

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

28 September 2011 at 8:26am Community Member, 288 Posts

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

28 September 2011 at 8:34am Community Member, 99 Posts

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

Cheers! :-)

Avatar
LinseyM

14 October 2011 at 11:57pm Community Member, 99 Posts

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