Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Contact Form - From Address Help

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

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

Page: 1 2
Go to End
Author Topic: 3287 Views
  • Liam
    Avatar
    Community Member
    470 Posts

    Contact Form - From Address Help Link to this post

    Hello,

    I just need a simple contact form that a user can fill out, and the results are emailed to the admin. I could easily use the user defined forms cause it's good enough, except I'd really like the from address to be the user's email, not what's defined in the config file. That way when the email comes in, the admin can read the email and just click reply in their email program to respond to it.

    I'd assume I have to code my own, but being a designer first, I'd love as much info on this as possible. Example code would be great.

    Cheers,

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Contact Form - From Address Help Link to this post

    Quickly whipped something up here:

    http://pastie.org/239113

    Should be somewhat self-explanatory. You'd put a template in the mysite/templates/email directory, and you can loop over the form data in there by using something like:

    <% control FormData %>
    Name: $Name
    <% end_control %>

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Contact Form - From Address Help Link to this post

    Thanks so much for the help. That's more than I expected, but fantastic. Saves me a lot of work.

    I followed the steps, and the form shows up on the newly created contact page type. When I go to submit the form though, all I get is a blank screen. The form doesn't email and nothing is shown.

    Am I missing something or is there an error in the code?

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Contact Form - From Address Help Link to this post

    I still haven't been able to figure out why my form won't submit.

    Also, how do I define a custom template for this form? I tried using the function forTemplate() in the class, but even after a /flush=1 I still get the standard form. I included the new template in the /templates/includes directory.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Contact Form - From Address Help Link to this post

    Submit problem:

    Try putting die('here') at the end of the doForm() function. Try submit again and see if it spits out "here" in the browser.

    Template:

    You need to create a new class for your form if you want to define a custom template. This means doing something like this:

    http://pastie.org/243869

    Then, in the ContactPage_Controller class, remove the doForm() function as per my previous example, and replace the Form() function with this one:

    function Form() {
       return new MyForm($this, 'Form');
    }

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Contact Form - From Address Help Link to this post

    Okay I got the new template up and running perfectly with the new code you showed.

    The form still doesn't submit though. I tried putting in the die() function at the end of the doform function, but it didn't show up. I then tried putting it at the start of the doform() function, and still nothing.

    Here is the code I have in my ContactPage.php http://pastie.org/244292

    It seems like a small error somewhere, but nothing stands out to me.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Contact Form - From Address Help Link to this post

    Hmm, that's odd.

    Here's what I did:

    1. I took your code from here: http://pastie.org/243869

    2. Pasted it into ContactPage.php inside mysite/code, ran db/build?flush=1

    3. Applied ContactPage to any page in the CMS, and viewed the page to show the form.

    4. Tried submitting the form, and it worked fine for me - I put a die('here') at the top of doForm() and it showed that text in the browser after submitting.

    Is this what you're doing as well?

    Here's the code that works for me, copied and pasted back for sanity reasons: http://pastie.org/244406

    Cheers,
    Sean

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Contact Form - From Address Help Link to this post

    That's pretty much what I've done. I've run /db/build?flush=1 after installing the code and assigning the page type to the contact page.

    The form shows up no problem, it's just the submitting it where it chokes. The URL after is /contacts/?executeForm=Form and is just a white page.

    The only thing I can think of is that I'm running 2.2.2 stable, not trunk. Not sure if that has anything to do with it.

    Emails are sending as I setup a custom newsletter after learning forms from this example. Users were subscribed perfectly and some sample newsletter were sent and received.

    3287 Views
Page: 1 2
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.