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

Steven Velazquez
Steven Velazquez
7,278 Points

POST routing issue! please help

I am having trouble with the routing of the sites. When I hover over the About and Contact links, at first it recognizes both links as a part of the page. After I click a link it then changes and recognizes as as .html link. Additionally, when I try to edit the form action="" method="post" in the contact.twig page, it doesn't recognize the change in the chrome developer inspection tool. Because of this, when I try to submit a pseudo form, the array page does not show up. I have been stuck for hours and even tried copy pasting the original coding but it's still not working. Here's some of the coding if that helps:

index.php:

<?php
$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());
});

main.twig:
        <a href="{{ baseUrl() }}" class="selected">About</a>
        <a href="{{ siteUrl('/contact') }}">Contact</a>

contact.twig:
    <form action="" method="post">
      <fieldset>
        <input name="name" type="text" placeholder="Full Name">
        <input name="email" type="email" placeholder="Email Address">
        <textarea name="msg" placeholder="Your message..."></textarea>
      </fieldset>
      <input type="submit" class="button">
    </form>

Thanks for the help :)

edited to format code.

Jason S
Jason S
16,247 Points

what happens if you get rid of the index.html and contact.html?

4 Answers

Yo!

<?php

$app->request->post()

will indeed dump out the entire post data. The post method accepts two optional arguments - $key and $default. You can ask for a specific key of the post data (and provide a default if it doesn't exist) or you can grab the entire post data array.

You said that the navigation links are changing between pages? Just make sure that all of you're pages are extending from main.twig - in other words, make sure are they using all the same code!

Again, if you're making changes in the code to your twig template, have you double checked you're actually pointing to the correct twig page etc...? You can test easily by doing something like deleting all the code and refreshing the browser. Does it all disappear?

I am learning a lot from you today. :)

We need to see code to really help I think.

Nothing is obviously wrong with your code to me, but this is sensitive, so we need to see the entire files. If this is in Workspaces, then please post a snapshot. To do that, click the camera icon in the upper right and follow the steps until you get a URL you can copy and paste here.

The other thing that I don't know will work is when you var_dump() $app->request->post(). I would test that by trying var_dump($app->request->post('email'));.

I am out of time right now, but I have some other ideas I will look at and post later today if I can.

Steven Velazquez
Steven Velazquez
7,278 Points

https://w.trhou.se/nebl7ht0at

Here is the URL for my workspace. I followed both of your suggestions and made sure my twig pages were extending main.twig but still no success. I appreciate the help!! Thanks a lot guys!

Luke Dawes
Luke Dawes
9,739 Points

This is an old thread, but most of the issues I've had with the course so far were eliminated when I ensured that I was installing the same versions as Hampton (Monolog 1.22, Slim 2.6, and Twig 1.18). The only exception was Slim/Views -- Hampton installed 0.1, whereas 0.1.1 included a patch that fixed a common issue faced by Treehouse students (can't remember exactly which issue).

With this issue, I deleted the index.html and contact.html files, leaving on the index.php, about.twig and contact.twig files, which I don't think was explicitly advised in the video. This immediately fixed the issue that you experienced, Steven, and the issue(s) experienced by Yvggeniy Romanskiy. I can now see the same POST data as Hampton shows in the video.

Good luck!