Onepager contactform problem

Hi,
i use @bastianallgeier contactform on a onepager like this.
Now try to get this work but if I press the submit the site is reloading and no mail is outgoing.
Inside the templates the $page data is calling by $data->… could it be that I have to change parameters on the controlerfile on return function($site, $pages, $page)?
Here is my controlerfile

Cheers

The example code does not contain $data-> anywhere. The $data variable gets set to an array with the form data in the controller.

There’s one change in your controller that’s missing though: The last line needs to be changed to return compact('contactalert'); and the variable name needs to be changed in the template as well.

Hello Lukas,

return compact('contactalert');that I have forget to change.
In my template I have changed the variables.
template
What I have tried is to add the $data to the Parameters and also to replace the $page through $data.
But also both variants only reload the site.
controler

I suppose your contactform.php is a snippet that goes into home.php? Then your controller code should be in the home.php controller as well, not in a contact.php that is never called because there is no such template.

1 Like

I love you Texnixe :)heart_eyes:
This was the solution!

Now I have to solve the problem that I need a message to show that the mail is outgoing.

If you don’t want to redirect to another page, you have to change the controller code slightly:

...
      if($contactemail->send()) {
        $success = 'Thank you for your message';
      // add the error to the alert list if it failed
      } else {
        $contactalert = array($contactemail->error());
      }
...

Don’t forget to return the new variable; then you can use it in your template …

1 Like

Sorry for reactivating this thread, but that is exactly what I am looking for. Implemented the above contact form in a one-pager from the cookbook and I don´t want to leave page. Works so farexcept the last part. How do I need to return the “$success” variable and use it in the template? Many thanks

You have to return it in the return statement of your one-pager controller, where you have something like

return function($site, $pages, $page, $data) {
  
// all your controller code
  
     $success = null;

     if($contactemail->send()) {
        $success = 'Thank you for your message';
      // add the error to the alert list if it failed
      } else {
        $contactalert = array($contactemail->error());
      }

  return compact('variable1', 'contactalert', 'success');
};
1 Like