Jump to content

contact form in pw


Arkster
 Share

Recommended Posts

Hello,

I'm trying to create a contact form within processwire. What is the best way to go about this? Contact forms are on many pages on the site. I created a template with php in it that I'd use for a static site to handle the form and send an email, but it is not working.

I found your module here: http://processwire.com/talk/index.php/topic,75.msg362.html#msg362 but I am having trouble understanding how to use it exactly...

  • In admin, I created the fields contactName, contactEmail, and contactMessage
  • I created a new template contactProcess and assigned those fields to that template (just made an empty template file and added it via admin. Is that right?)
  • put the code below where my static contact form used to be (in php tags of course):

$form = $modules->get('FormTemplateProcessor'); 
$form->template = $templates->get('contactProcess'); // required
$form->requiredFields = array('contactName', 'contactMessage');
$form->email = 'your@email.com'; // optional, sends form as email
echo $contactForm->render(); // draw form or process submitted form

I think I must be doing something really stupid and/or missing something really obvious.

Thank you for any help you can provide. Also, just gotta say that I really love processwire. I love how it lets me use php.

Link to comment
Share on other sites

Your code looks right to me, as do the steps you mentioned. When you say it's not working, can you describe more? Do you ever see the form that it outputs? Or is it just that you never get an email? I'm not clear about where it stops working.

I also suggest editing /site/config.php and temporarily setting $config->debug = true (near the bottom of the file). That will ensure that you see error messages during development.

Also, if you have a URL where I can see it in the context of the rest of the page, that may help (and feel feel to PM it to me if you prefer not to post here).

Link to comment
Share on other sites

Thanks for your response Ryan. I turned debug mode on, and this is the output:

Notice: Undefined variable: contactForm in /hsphere/local/home/freddysfatfender/freddysff.inconceivabledesign.com/site/templates/contact.php on line 24 Fatal error: Call to a member function render() on a non-object in /hsphere/local/home/freddysfatfender/freddysff.inconceivabledesign.com/site/templates/contact.php on line 24

Doesn't that look like maybe the module isn't installed correctly/working? I installed it through the admin.

url:

freddysff.inconceivabledesign.com

click "contact" in the footer

let me know if you want login details if you want to take a look under the hood (though I don't expect you to take time out of your day to do that).

Link to comment
Share on other sites

side note: There is a contact form on all of the vehicle pages. So when this goes live there is going to be a lot of different places a contact form resides. My idea was to have a hidden field with the value of the page title - so that whoever is getting the emails know what page they sent the form from.

Would this be possible using this module? If not, maybe I should search for a different way of doing this?

Link to comment
Share on other sites

I missed it the snippet you pasted before, but PHP is right, $contactForm is not a variable. It should be $form instead, i.e.

$form->render();

It looks like my example at the top of the module file is incorrect (sorry about that). But if you replace that $contactForm with $form, that should solve it.

For your hidden field (contactPage), add it to your form template as a text field. But hide it from your stylesheet so that it doesn't appear in your form (even though it's still technically there).Then I think you'll want to populate the value into $input->post manually before rendering the form, i.e.

<?php
$input->post->contactPage = $page->title;
$form = $modules->get('FormTemplateProcessor'); 
...

As a side note, since your contactPage field is hidden by your stylesheet, you could do this:

<?php
if($input->post->contactPage) {
  // if this field is populated right now, it's very likely spam since a live user never would have seen it
  // but automated spam bots don't usually look at stylesheets to see if a field is visible
  $session->redirect("/"); // redirect spammer to homepage
}
$input->post->contactPage = $page->title;
$form = $modules->get('FormTemplateProcessor'); 
...

I haven't tried these examples, but think they should work. I should also mention that this module was really designed just as a proof-of-concept, rather than a full solution. It's somewhat bare and intended as a starting point for you to modify for your needs or build-upon. In most cases, I just build and process forms the old fashioned way in my own sites... can't beat the flexibility. :) But we'll be improving the form tools in ProcessWire more and more.

Link to comment
Share on other sites

Aha! Thank you...had I read a little further down the thread I would have seen that.

In the end I decided to flex my PHP muscles a little and just do an include to the below page wherever I want to form - like you say you can't beat the flexibility. I love getting paid to learn  :)

<?php 
echo "<a name='contactForm'></a>";
if ($_POST) {  //if already submitted
  $admin_email = "name@domain.tld";
  $uname = $input->post->name; //declare vars
  $uemail = $input->post->email;
  $umessage = $input->post->message;
  $upage = "<a href='$page->url'>$page->title</a>";
  $uname = trim($uname);
  $uname = $sanitizer->text($uname); //sanitize
  $allGood = true; //give allGood variable a default value of true
  
  include('./mailer.php');
   
  if ($allGood == true) { //the mail script determines that all fields are valid confirm success
       echo "<p>Thank you $uname. <br><br>Your message is on its way.</p>";
  } elseif ($allGood == false) { //if there was an error
    echo "
      <p>Correct the errors below before submitting.</p><br />
      <form method='post' action='$page->url#contactForm'>
              <label>your name</label>";
              if (isset($nameError)) {
                echo "<p class='error'>$nameError.</p>";
              }
              echo "
              <input name='name' type='text' size='35' value='$uname' />
              <label>your email</label>";
              if (isset($emailError)) {
                echo "<p class='error'>$emailError.</p>";
              }
              echo "
              <input name='email' type='text' size='35' value='$uemail' />
              <label>your message/phone</label> <textarea name='message' rows='3' cols='26' value='$umessage'></textarea>
              <input type='submit' value='submit' />
            </form>
  ";
  }
  else {
    echo "
      <p class='error'>There has been an error and your information was not passed along.<br /> Please email Fred at name@mac.com directly.</p>
  ";
  }
}
else { //if not already submitted
  echo "
    <form method='post' action='$page->url#contactForm'>
            <label>your name</label> <input name='name' type='text' size='35' />
            <label>your email</label> <input name='email' type='text' size='35' />
            <label>your message/phone</label> <textarea name='message' rows='3' cols='26'></textarea>
            <input type='submit' value='submit' />
          </form>
  ";
}
?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...