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 trialMichael McClintock
22,580 PointsPost data not rendering after submitting form within the contact page
Up until submitting the form, my program ran as expected. The last step in which Hampton selected 'Submit' (which displayed post data in the browser) uncovered an issue which I cannot figure out. I will paste my code below (irrelevant code omitted):
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());
});
$app->run();
main.twig
<?php
<header>
<h1>Ralph Waldo Emerson</h1>
<nav>
<a href="{{ baseUrl() }}" class="selected">About</a>
<a href="{{ siteUrl('/contact') }}">Contact</a>
</nav>
</header>
<main>
{% block content %}
{% endblock content %}
</main>
Lastly, contact.twig
<?php
{% extends 'main.twig' %}
{% block content %}
<main>
<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>
</main>
{% endblock content %}
3 Answers
Greg Kaleka
39,021 PointsHi Michael,
Looks to me like the problem is in your form tag; the action attribute is blank. Should be . Or /contact.
Let me know if that helps!
-Greg
Ted Sumner
Courses Plus Student 17,967 PointsHere is the final working project code:
index.php
<?php
// require Slim
require 'vendor/autoload.php';
// Set time zone
date_default_timezone_set('America/Los_Angeles');
//slim-views is required for Twig
//see https://github.com/slimphp/Slim-Views
//must install via composer
$app = new \Slim\Slim(array(
'view' => new \Slim\Views\Twig()
));
$view = $app->view();
$view->parserOptions = array(
'debug' => true
);
//the code below enables us to use the helpers below
//urlFor siteUrl baseUrl currentUrl
//documentation at https://github.com/slimphp/Slim-Views
$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){
$name = $app->request->post('name');
$email = $app->request->post('email');
$msg = $app->request->post('msg');
if(!empty($name) && !empty($email) && !empty($msg)) {
$cleanName = filter_var($name, FILTER_SANITIZE_STRING);
$cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
$cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING);
} else {
//message the user there was a problem
$app->redirect('/contact');
}
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance();
$message->setSubject('Email from Our Website');
$message->setFrom(array(
$cleanEmail => $cleanName
));
$message->setTo(array('teds@biblewordstudy.net'));
$message->setBody($cleanMsg);
$result = $mailer->send($message);
if($result>0) {
// send a message that says thank you
$app->redirect('/');
} else {
// send a message to the user that teh message failed to send
// log that there was an error
$app->redirect('/contact');
}
});
$app->run();
contact.twig
{% extends 'main.twig' %}
{% block content %}
<strong>Contact</strong>
<h2>Ralph Waldo Emerson</h2>
<p>Unfortately, Ralph Waldo Emerson has been deceased for over 100 years so he's not particularly expediant at replying to email but you can make an attempt below. However, if you require face to gravestone contact you can visit his remains at:</p>
<address>
<h4>Sleepy Hollow Cemetery</h4>
<p>34 Bedford Street<br>
Concord, MA 01742, United States<br>
<a href="https://www.google.com/maps/place/Sleepy+Hollow+Cemetery/@42.464126,-71.343098,15z/data=!4m2!3m1!1s0x0:0x9c41d0f83df689a6?sa=X&ei=ZCgLVZb5Io_hoASc7oHwCQ&ved=0CH0Q_BIwCw">Google Map</a></p>
</address>
<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>
{% endblock content %}
Greg Kaleka
39,021 PointsThat works with a blank action attribute?
Ted Sumner
Courses Plus Student 17,967 PointsYes. You can see the code live at http://vetsrights.org. What is the unexpected behavior you are seeing?
Ted Sumner
Courses Plus Student 17,967 PointsHere are several links regarding this project and some issues people run into:
https://teamtreehouse.com/community/get-404-error-when-trying-to-access-indexhtml-and-contacthtml
https://teamtreehouse.com/community/inconsistent-routing-and-helper-variable-output
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 Pointsformatted code quotes.