Jump to content

FrontendContact - Module for creating one or more contact forms on the frontend


Juergen
 Share

Recommended Posts

This is the next module beside the FrontendLoginRegister module which is based on the FrontendForms module.

As the name suggests, it has been designed to easily create a contact form for your site with the following characteristics:

  • Fast and easy integration of a contact form inside a template by using only one line of code
  • Show/hide certain fields of the form depending on your preferences and needs
  • Beside the default fields you will be able to extend the form with additional fields if needed
  • Highly customizable (change order of fields, add custom CSS classes,...)
  • Run as many forms on one page as you want
  • Possibility to offer file upload to upload multiple files, which can be sent as attachments
  • Usage of all the benefits of FrontendForms (fe. CAPTCHA, various security settings,...)
  • Multi-language
  • IP of the sender will be send with the email too, so you can use it for IP-blocking if you will get a lot of spam from a certain IP

configuration.thumb.png.ffd876974daf9bd4410225f596aeed16.png

To render a complete working contact form, you will only need to add this line of code to your template:

echo $modules->get('FrontendContact')->render();

form.thumb.png.68979a50868c1f477f9c7a5bd89acd02.png

The output can differ from the image above, because it depends on your settings and customizations, but it will looks like similar to the form in the image.

This module is completely new and therefore alpha stage - so be aware of using it on live sites!

It works as expected in my tests, but it will need further testing.

You can download the module here: FrontendContact or you can install it via the Processwire upgrade-module from the module directory. Alternatively you will find all files inside GitHub.

You will also find a more detailed description on the the download page.

As always, please report issues or wishes here or directly on GitHub.

 

Thanks for testing!

  • Like 7
Link to comment
Share on other sites

Hello Juergen, I tried to install the module, but I have this error when I tried to add it from the admin :

Erreur d'analyse (parse): syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) (ligne 22 de site/modules/FrontendContact/FrontendContact.module) 

I also have this error when I tried to install the FrontendForms :

Erreur d'analyse (parse): syntax error, unexpected 'const' (T_CONST) (ligne 43 de site/modules/FrontendForms/FrontendForms.module) 

 

Link to comment
Share on other sites

  • 3 weeks later...

No other issues have been reported till now and I have tested the module once more and have fixed some problems. Now the module works as expected in my case without problems.

Today I have added the module to the module directory - please download it from there after it has been published.

Thanks!

Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...

Version 1.0.2 is out now!

This new version includes a new privacy feature: you can now not only decide whether or not to display an "Accept Privacy" checkbox - you can now choose to display a checkbox, a text-only version, or nothing at all.

IMG_20231029_105650.jpg.26245339461ee0cac5b80dd06a2018f6.jpg

The idea for this new feature comes from a request from @Chris-PW on the forum, who brought to my attention that a checkbox to accept privacy is no longer recommended and a simple text like "By submitting the form you agree to our privacy policy" is much more common nowadays.

With the new feature, you can use the type of privacy notices you want.

Before you update this module, you should first update FrontendForms to the latest version, because it contains new methods used by this module. Otherwise, ProcessWire will not let you install the new module version.

After updating to the latest version, go to the module configuration, open the Privacy Field tab to make your settings, and save the module configuration.

Go to the frontend and verify that the contact form displays correctly with the new settings.

As always, make sure everything works as expected and please post any issues you encounter directly to GitHub.

Have a nice Sunday!😀

 

Link to comment
Share on other sites

  • 1 month later...
On 8/29/2023 at 4:32 PM, Flashmaster82 said:

Are you planning on implementing save to new page (admin) feature? Also a feature that would be nice then is to choose if send only to a new page and not to email.

Yes, this would be awesome actually! Thanks for suggesting it.

Link to comment
Share on other sites

  • 2 weeks later...

Hello @Flashmaster82

Please copy this code to your template:

$form = $modules->get('FrontendContact')->getForm();

// phone field
$phone = new \FrontendForms\InputText('phone');
$phone->setLabel('Phone number');
$phone->setRule('numeric');
$phone->setRule('required');
// add this field after the name field
$form->addAfter($phone, $form->getFormElementByName('surname'));


$service = new \FrontendForms\Select('service');
$service->setLabel('Services');
$service->addOption('Service 1', 'service1');
$service->addOption('Service 2', 'service2');
$service->addOption('Service 3', 'service3');
$service->setRule('required');
$form->addBefore($service, $form->getFormElementByName('message'));

echo $form->render();// Render the form

 

image.thumb.png.952e599db8668d764e3e46883e2db70a.png

I have disabled following forms in the backend configuration of this module, because they were not on your list: gender, subject and privacy.

BTW, you have not written if the dropdown should be a single select or a multi-select, so I have chosen a single select.

To position the custom form fields inside the form I have used the addBefore() and addAfter() method. You will find an explanation about using custom fields on GitHub.

If you want to know, how you create custom fields you will find a lot of examples inside the repository of FrontendForms.

Hope this helps!

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Thanks for the reply. I changed to your other module Frontendform and everything works accept im not receiving any email? the email in this code changed btw.

 

<?php namespace Processwire; ?>
<div class="container col-5 tmar10">
<?php

$form = new \FrontendForms\Form('contactform');
$form->setSuccessMsg(__('Your message was submitted successfully!'));
$form->setErrorMsg(__('Sorry, but there are errors!'));

$firstname = new \FrontendForms\InputText('scf_firstname');
$firstname->setLabel(__($fields->get('scf_firstname')->$label));
$firstname->setRule('required');
$firstname->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($firstname);

$lastname = new \FrontendForms\InputText('scf_lastname');
$lastname->setLabel(__($fields->get('scf_lastname')->$label));
$lastname->setRule('required');
$lastname->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($lastname);

$phone = new \FrontendForms\InputText('scf_phone');
$phone->setLabel(__($fields->get('scf_phone')->$label));
$phone->setRule('required');
$phone->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($phone);

$email = new \FrontendForms\InputText('scf_email');
$email->setLabel(__($fields->get('scf_email')->$label));
$email->setRule('required');
$email->setRule('email');
$email->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($email);

$legalservice = new \FrontendForms\Select('scf_legalservice');
$legalservice->setLabel(__($fields->get('scf_legalservice')->$label));
// Add options dynamically based on existing pages
$legalServices = wire('pages')->find('template=legal_service');
foreach ($legalServices as $service) {
    $legalservice->addOption($service->title, $service->id);
}
$legalservice->setAttribute('class', 'w3-select w3-border w3-margin-bottom');
$form->add($legalservice);

$message = new \FrontendForms\Textarea('scf_message');
$message->setLabel(__($fields->get('scf_message')->$label));
$message->setRule('required');
$message->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($message);

// Note: scf_date is not added as a form field, but it will be set when saving the new page

$button = new \FrontendForms\Button('submit');
$button->setAttribute('value', __($fields->get('label_send')->$label));
$button->setAttribute('class', 'w3-btn w3-round w3-blue');
$button->addWrapper()->setAttribute('class', 'w3-padding-24 w3-center');
$form->add($button);

if ($form->isValid()) {
    // Form data is valid, save to a new page
    $p = new Page();
    $p->template = 'contact_inquiry';
    $p->parent = wire('pages')->get('template=contact');
    $p->title = $form->getValue('scf_firstname') . ' ' . $form->getValue('scf_lastname');
    $p->scf_firstname = $form->getValue('scf_firstname');
    $p->scf_lastname = $form->getValue('scf_lastname');
    $p->scf_phone = $form->getValue('scf_phone');
    $p->scf_email = $form->getValue('scf_email');
    $p->scf_legalservice = $form->getValue('scf_legalservice');
    $p->scf_message = $form->getValue('scf_message');
    
    // Set the scf_date field to the current date and time
    $p->scf_date = time();
    
    $p->save();

    // send an email
    $m = wireMail();
    $m->to('myemail@gmail.com');
    $m->fromName($form->getValue('scf_firstname') . ' ' . $form->getValue('scf_lastname'));
    $m->from($form->getValue('scf_email'));
    $m->subject(_('New message from the contact form'));
    $m->bodyHTML($form->getValue('scf_message') . ' <br><br> ' . $form->getValue('scf_address'));
    $m->send();
    
    // Redirect to the success page
    wire('session')->redirect(wire('config')->urls->root . 'forfragan-skickades/');
}

echo $form->render();

?>
</div>

 

Link to comment
Share on other sites

Hello @Flashmaster82

Your code seems to be OK. I have tested it (except the creation of the new page) and I received the mail as expected (tested on localhost with Xampp).

image.thumb.jpeg.678bef22976fc66f59d35b0c6cf491b6.jpeg

Sometimes it will take some time, until the provider sends the mail to your inbox.

You can also try the following:

// replace
$m->send()

// with
if($m->send()){
  print_r('sent');
}

If the mail has been sent, the print_r will be executed, so you can be for sure that the mail was sent successfully. If you do not receive anything the problem must be afterwards (fe mail configuration on your host,...)

  • Like 1
Link to comment
Share on other sites

Thanks again for the reply, i tried the update it will make a new page and there is a success message. Im using Frontendform module btw. But im not receiving any mail? hmm.. Is there any settings in the Frontendform module that is important? noreply email? Do i need to install any other module to make this work? i only have the Frontendform module.

Link to comment
Share on other sites

 

6 minutes ago, Flashmaster82 said:

Is there any settings in the Frontendform module that is important? noreply email?

No, the sending of emails is not part of the module, it is part of the ProcessWire WireMail class and has nothing to do with FrontendForms.

I have only included 3 parameters (mail template, date format and time format) for sending mails and they have nothing to do with the sending process.

Does print_r() output anything - this will be an important information!!

Be sure that the receiver email address is written correctly, otherwise you will not get any mails!!

Link to comment
Share on other sites

No, only the pure PHP WireMail class which is included by default. There is no need to use an additional module.

You have not answered my question: Does print_r() output anything?? It should return a number higher than 0; Please disable the redirect for this case!!

Please check your email address once more, or try to use another email address. In the past I had a writing mistake in an email address and I have not found this mistake for a while.

Do you have TracyDebugger installed?

Link to comment
Share on other sites

Alternatively you can try this simple code to see if mail sending works:

$form = new \FrontendForms\Form('myForm');

$name = new \FrontendForms\InputText('lastname');
$name->setLabel('Last Name');
$name->setRule('required');
$form->add($name);

$button = new \FrontendForms\Button('submit');
$button->setAttribute('value', 'Send');
$form->add($button);

if($form->isValid()){
    $m = wireMail();
    $m->to('myemail@example.com'); // please change it
    $m->subject('Test message');
    $m->bodyHTML('My body text');
    if($m->send()){
        bd('send');
    }
}

Please change the email address!!!!

Link to comment
Share on other sites

14 minutes ago, Flashmaster82 said:

Shouldn´t i insert some send email "noreply@...

This is not necessary!!!

17 minutes ago, Flashmaster82 said:

The print output the success message. I tried with your simple form and it print out Thank you for your message.

You misunderstand me completely! I do not want to know if the success message "Thank you for you message"  will be printed out - I know that this will be printed out, but it has nothing to do with sending an email. It only gives you a message, that the form is validated correctly.

I want to know if the word "sent" from the print_r() will be appear on the screen!!!

9 hours ago, Juergen said:
if($m->send()){
  print_r('sent');
}

BTW I have used a TracyDebugger function in my last code example. So this will not work if you have TracyDebugger not installed.

Please use the code above with print_r('sent') and not bd('send').

Can you post a screenshot before and after the form has been submitted, so I can see what is going on on the screen?

Link to comment
Share on other sites

4 minutes ago, Flashmaster82 said:

Ok but the sending of the email if what not working right now?

I am sorry, but I cannot understand what you mean?!? Post only a screenshot of the page with the form before you push the send button and afterwards. Please use the form with my code!!

This is the code, what we are testing at the moment:

$form = new \FrontendForms\Form('myForm');

$name = new \FrontendForms\InputText('lastname');
$name->setLabel('Last Name');
$name->setRule('required');
$form->add($name);

$button = new \FrontendForms\Button('submit');
$button->setAttribute('value', 'Send');
$form->add($button);

if($form->isValid()){
    $m = wireMail();
    $m->to('myemail@example.com'); // please change it
    $m->subject('Test message');
    $m->bodyHTML('My body text');
    if($m->send()){
        print_r('send');
    }
}
Link to comment
Share on other sites

This is so strange. Now with your code im receiving email. But with this code i will not receive any email?

<?php namespace Processwire; ?>
<div class="container col-5 tmar10">
    
<?php
$form = new \FrontendForms\Form('myForm');

$firstname = new \FrontendForms\InputText('scf_firstname');
$firstname->setLabel(__($fields->get('scf_firstname')->label));
$firstname->setRule('required');
$firstname->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($firstname);

$lastname = new \FrontendForms\InputText('scf_lastname');
$lastname->setLabel(__($fields->get('scf_lastname')->label));
$lastname->setRule('required');
$lastname->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($lastname);

$phone = new \FrontendForms\InputText('scf_phone');
$phone->setLabel(__($fields->get('scf_phone')->label));
$phone->setRule('required');
$phone->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($phone);

$email = new \FrontendForms\InputText('scf_email');
$email->setLabel(__($fields->get('scf_email')->label));
$email->setRule('required');
$email->setRule('email');
$email->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($email);

$legalservice = new \FrontendForms\Select('scf_legalservice');
$legalservice->setLabel(__($fields->get('scf_legalservice')->label));
// Add options dynamically based on existing pages
$legalServices = wire('pages')->find('template=legal_service');
foreach ($legalServices as $service) {
    $legalservice->addOption($service->title, $service->id);
}
$legalservice->setAttribute('class', 'w3-select w3-border w3-margin-bottom');
$form->add($legalservice);

$message = new \FrontendForms\Textarea('scf_message');
$message->setLabel(__($fields->get('scf_message')->label));
$message->setRule('required');
$message->setAttribute('class', 'w3-input w3-border w3-margin-bottom');
$form->add($message);

$button = new \FrontendForms\Button('submit');
$button->setAttribute('value', __($fields->get('label_send')->label));
$button->setAttribute('class', 'w3-btn w3-round w3-blue');
$button->addWrapper()->setAttribute('class', 'w3-padding-24 w3-center');
$form->add($button);
    
if ($form->isValid()) {
    // Form data is valid, save to a new page
    $p = new Page();
    $p->template = 'contact_inquiry';
    $p->parent = wire('pages')->get('template=contact');
    $p->title = $form->getValue('scf_firstname') . ' ' . $form->getValue('scf_lastname');
    $p->scf_firstname = $form->getValue('scf_firstname');
    $p->scf_lastname = $form->getValue('scf_lastname');
    $p->scf_phone = $form->getValue('scf_phone');
    $p->scf_email = $form->getValue('scf_email');
    $p->scf_legalservice = $form->getValue('scf_legalservice');
    $p->scf_message = $form->getValue('scf_message');
    
    // Set the scf_date field to the current date and time
    $p->scf_date = time();
    
    $p->save();

    // send an email
    $m = wireMail();
    $m->to('info@....com');
    $m->subject(_('New message from the contact form'));
    $m->fromName($form->getValue('scf_firstname') . ' ' . $form->getValue('scf_lastname'));
    $m->from('noreply@....se');

    // Customize the email message body
    $messageBody = "First Name: " . $form->getValue('scf_firstname') . "<br>";
    $messageBody .= "Last Name: " . $form->getValue('scf_lastname') . "<br>";
    $messageBody .= "Phone: " . $form->getValue('scf_phone') . "<br>";
    $messageBody .= "Email: " . $form->getValue('scf_email') . "<br>";
    $messageBody .= "Legal Service: " . $form->getValue('scf_legalservice') . "<br>";
    $messageBody .= "Message: " . $form->getValue('scf_message') . "<br>";

    $m->bodyHTML($messageBody);
    if($m->send()){
        print_r('send');
    }
}
echo $form->render();
?>
</div>

Email is changed. I have got some help from ai chat.

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

×
×
  • Create New...