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
ryanandrews
7,822 PointsPlease Take a look at my code and find my error because I Can't.
I am finishing up with the if/else statements for my form on "How to build a Simple PHP application and I can't seem to find why it is not working. From what I can see I have my code matching the one in Randy's video. I think I have everything there but it is not redirecting to the get variable statusthanks and showing the thank you message, instead it is simply going the form.
<?php
if ($_Server["REQUEST METHOD"] == "POST") { $name = $_POST["name"]; $email = $_POST["email"]; $message = $_POST["message"]; $email_body = ""; $email_body = $email_body . "Name: " . $name . "\n"; $email_body = $email_body . "Email: " . $email . "\n"; $email_body = $email_body . "Message: " . $message;
// ToDO: Send Email
header("Location: contact.php?status=thanks");
exit;
}
?><?php $pageTitle = "Contact Mike"; $section = "contact"; include('inc/header.php'); ?>
<div class="section page">
<div class="wrapper">
<h1>Contact</h1>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly.</p>
<?php } else { ?>
<p>I’d love to hear from you! Complete the form to send me an email.</p>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="email" name="email" id="email">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
</table>
<input type="submit" value="send">
</form>
<?php } ?>
</div>
</div>
<?php include('inc/footer.php'); ?>
Also if there is a better way then to just copy and paste my code on to the forum please let me know.
Thank you for your help, I have given up staring at this code.
4 Answers
Michael Nguyen
6,138 PointsI think you have a typo. Use $_SERVER['REQUEST_METHOD'] == "POST"
ryanandrews
7,822 PointsOk I noticed my code didn't post very well, tips?
James Barnett
39,199 PointsThe forum uses markdown to correctly format code, check out this thread on how to type code in the forum for some examples.
ryanandrews
7,822 PointsThanks James.
ryanandrews
7,822 Points<?php
if ($_Server["REQUEST METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Message: " . $message;
// ToDO: Send Email
header("Location: contact.php?status=thanks");
exit;
}
?><?php
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>
<div class="section page">
<div class="wrapper">
<h1>Contact</h1>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly.</p>
<?php } else { ?>
<p>I’d love to hear from you! Complete the form to send me an email.</p>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="email" name="email" id="email">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
<tr>
<th>
<label for="reason">Reason</label>
</th>
<td>
<select name="Reasons" id="Reason">
<option value="Blank">---</option>
<option value="Size">Size Inquiry</option>
<option value="Payment">Payment</option>
<option value="Other">Other</option>
</select
</td>
</tr>
</table>
<input type="submit" value="send">
</form>
<?php } ?>
</div>
</div>
<?php include('inc/footer.php'); ?>
ryanandrews
7,822 Pointsryanandrews
7,822 PointsThanks for the try but no that is not doing it.
ryanandrews
7,822 Pointsryanandrews
7,822 PointsI am so sorry Michael, you were right. I still could not see my mistake even when you wrote it out for me. Thank you for your help....I feel like an idiot but I am glad to be able to move forward. It did make me review everything in that lesson about 5x.
Michael Nguyen
6,138 PointsMichael Nguyen
6,138 PointsI am glad it works out for you. This is a nature of programming. Don]t bother about it too much. Sometime the solutions are right in front of me and I only get it after weeks or even months :)