Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Building Websites with PHP Contact Form & Sending Email POST Route & Data

Yvggeniy Romanskiy
Yvggeniy Romanskiy
2,803 Points

Can't Get it to post

When i try to use post to var_dump the array of the name, email and msg it just wont post. instead everything I'm writing in the name, email and msg showing up in the address bar. and the page is refreshing.

later when i need to send an email it wont send it and wont redirect me to the main page. and it wont send any errors, all that happens is i see the form i filled in the address bar.

Like this:

treehouse-app.com/contact?name=Jhon+pliskin&email=pliskin%40mother.bace&msg=Special+message.+

<?php
require __DIR__ . '/vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

//$log = new Logger('name');
//$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
//$log->addWarning('Oh Noos.');

$app = new \Slim\Slim(array(
  'view' => new \Slim\Views\Twig()
));

$view = $app->view();
$view->parserOptions = array(
    'debug' => true
);

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

$app->get('/', function() use($app){
  $app->render('about.twig');

})->name('home');

$app->get('/contact', function() use($app){
  $app->render('contact.twig');
})->name('contact');

$app->post('/contact', function() use($app){
    var_dump($app->request->post());
});      

$app->run();

Here is my project https://w.trhou.se/up0fzwlad3

3 Answers

Ahmed Aly
Ahmed Aly
468 Points

The first thing I figured out is that you did not set the form correctly to make a post or get request. So it makes a get request by default, although it needs to be a post request, to process the data. Once you solve this, I think maybe it would work, or at least it will be easier to work.

Yvggeniy Romanskiy
Yvggeniy Romanskiy
2,803 Points

Wow I'm so blind!!! thank you very much! you've helped me a lot!

I did messed up the form....

instead of:

     <form action="" method="post"> /*<---- this supposed to  be here  */
        <fieldset>
          <input name="name" type="text" placeholder="Name">
          <input name="email" type="email" placeholder="Email Address">
          <textarea name="msg" placeholder="Your message..."></textarea>
        </fieldset>
        <input type="submit" class="button">
      </form>

I did:

      <form>
        <fieldset action="" method="post"> /* <---- and not here   */
          <input name="name" type="text" placeholder="Name">
          <input name="email" type="email" placeholder="Email Address">
          <textarea name="msg" placeholder="Your message..."></textarea>
        </fieldset>
        <input type="submit" class="button">
      </form>
Ahmed Aly
Ahmed Aly
468 Points

I forked it into my workspace, and I will try to debug it to see why it does not work exactly.

Yvggeniy Romanskiy
Yvggeniy Romanskiy
2,803 Points

How so? i followed the video exactly, can you point to what i did not set correctly? i personally think it might be connected to the version of Slim or Twig I'm using.

Edit: no its not a version problem, just tried to install the exact same versions of all the libraries and I'm getting the same problem.