External Form redirects to access denied and doesnt send email

Permalink
I wrote this simple form that is meant for a client to put in their information and allow the business to get it through email.

The code worked fine but I accidentally lost it when C5 rebooted and so I'm remaking it.

I thought I made an exact copy though when I click submit the page is redirected to an access denied page and no email is sent.

I'm fairly new to PHP and C5 (as you can tell with how basic the code is) so it may be a simple problem though I haven't been able to find anything out.

I've tried changing the action and renaming my files along with toying with redirecting it to the website page but no email is sent if I do that.

Here's my code. Thanks in advance for any help!

<?php
namespace Application\Block\ExternalForm\Form\Controller;
use Core;
use Loader;
use Concrete\Core\Controller\AbstractController;
class ContactusForm extends AbstractController
{
    public function action_contactus_form($bID = false)
    {
        if ($this->bID == $bID) {
        $form = Loader::helper("form");
        $name = $_POST['name'];
        $visitor_email = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];


<?php
$form = Loader::helper('form');
defined('C5_EXECUTE') or die("Access Denied.");
?>
<form method="post" class="form" action="<?=$view->action('contactus_form')?>">
    <input type="text" name="name" placeholder="Your Name: " required/>
    <br />
    <input type="email" name="email" placeholder="Your Email: " required/>
    <br />
    <input type="tel" name="phone" placeholder="Your Phone Number: 012-345-6789 (optional) "pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
    <br />
    <textarea name="message" placeholder="Your Message for us!" cols="3" rows="3" required>
</form>

View Replies:
linuxoid replied on at Permalink Reply
linuxoid
At first glance, try to replace the 'return true' for '$this->view()' in the controller action function.

What's the header for?

What's the $response?

You may have a look athttps://www.concrete5.org/marketplace/addons/contact-form1...

e.g.
public function action_submit($token = false, $bID = false) 
{
   if ($this->bID != $bID) {
      return false;
   }
   if ($this->app->make('token')->validate('contact_form'.$this->post('buid'), $token)) {
      if ($this->validate_form()) {
         $this->mail_form();
         ...
      }
      else {
         ...
      }
   }
   else {
spencerld88 replied on at Permalink Reply
The $response is an alert if it sends through I think it was in the test form in the concrete external form file so I thought to try and add it to maybe help. I have deleted it realizing it's unnecessary.

The header is supposed to show the location of the page that the form is in. I don't know if I should delete that also. I tried to see if it changed anything and it didn't have any impact on the issue I'm experiencing.
linuxoid replied on at Permalink Reply
linuxoid
Try the following approach (design to suit).

view.php:
<?php defined('C5_EXECUTE') or die("Access Denied.");
$form_action = $view->action('submit', $app->make('token')->generate('contact_form'));
?>
<form id="contact_form" 
    action="<?php echo $form_action; ?>" 
    method="post" 
    accept-charset="utf-8">
    ...
</form>

controller.php:
public function action_submit($token = false, $bID = false) 
{
    $data = $this->request->request->all();
    if ($this->bID != $bID) {
        return false;
    }
    elseif ($this->app->make('token')->validate('contact_form', $token)) {
        if ($this->ValidateForm($data)) {
            $this->MailForm($data);
            // do something after form sent
        }
        else {
            // do something if form validation failed
        }
    }

You can also make it fancy with Ajax so that the page doesn't refresh after submitting the form. The addon does that anyway.
spencerld88 replied on at Permalink Reply
Alright, I'll try the code in my php. It may be a little bit before I know if I'm failing or not since I don't know too much I'll have to trial and error and probably change the code.

I strongly appreciate the help because I've been stuck for a couple of weeks now on this specific problem!
spencerld88 replied on at Permalink Reply
So when I try it the code $app->make("token") pulls an error named " Call to a member function make() on null " pops up.

I've tried including code in a function make() but it won't register the code in my view.php file.

<?php defined('C5_EXECUTE') or die("Access Denied.");
$form_action = $view->action('submit', $app->make('token')->generate('contactus_form'));
?>
<form id="form" action="<?php echo $form_action; ?>" method="post" accept-charset="utf-8">
    <input type="text" name="name" placeholder="Your Name:" />
    <span class ="error"><?= $name_error ?></span>
    <br />
    <input type="email" name="email" placeholder="Your Email:" />
    <span class ="error"><?= $email_error ?></span>
    <br />
    <input type="tel" name="phone" placeholder="Your Phone Number: 012-345-6789 (optional)" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
    <br />
    <textarea name="message" placeholder="Your Message for us!" cols="3" rows="3" ></textarea>
    <span class ="error"><?= $message_error ?></span>
    <br />
JohntheFish replied on at Permalink Reply
JohntheFish
You need to get the $app object, it doesn't necessarily already exist. See:
https://documentation.concrete5.org/developers/appendix/concrete5-ve...
spencerld88 replied on at Permalink Reply
Thank you! The error is no longer popping up but it is still routes to access denied page.