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 trialTzevai Chong
15,903 PointsProblem with contact form - redirects to blank page
I'm getting a blank page when I'm submitting my contact form. This happens even if I don't complete the fields. I've tried it on my localhost server and on my web server ([http://digitalshade.co.uk/lifelist/attend/]) but I can't figure out the issue? I've checked there's no whitespace. Am I missing something simple?!
My code is:
<?php
require_once("../inc/config.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$date = ($_POST["date"]);
if ($name == "" OR $email == "") {
$error_message = "Hmm, you seem to have forgotten your name or email!" ;
}
if (empty($date)) {
$error_message = "You haven't picked a date! We need to know when you can make it!";
}
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your form submission has an error.";
}
require_once(ROOT_PATH . "inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!isset($error_message) && !$mail->ValidateAddress($email)){
$error_message = "You must specify a valid email address.";
}
if (!isset($error_message)) {
$email_info = "";
$email_info = $email_info . "Name: " . $name . "<br/>";
$email_info = $email_info . "Email: " . $email . "<br/>";
$email_info = $email_info . "Dates: " . $date;
$mail->SetFrom($email, $name);
$address = "digitalshadeuk@gmail.com";
$mail->AddAddress($address, "Life List Party");
$mail->Subject = "Life List Party Info From " . $name;
$mail->MsgHTML($email_body);
if($mail->Send()) {
header("Location: " . BASE_URL . "attend/?status=thanks");
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
?><?php
include (ROOT_PATH . '/inc/header.php'); ?>
<div class="grid_12">
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<div class="infohead thanks">
<span>THANKS!</span><br />
We'll be in touch shortly to confirm the date :)
</div>
<?php } else { ?>
<?php
if (!isset($error_message)) {
echo '<div class="infohead">WANNA COME?</div>';
} else {
echo '<div class="message">' . $error_message . '</div>';
}
?>
<form method="post" action="<?php echo BASE_URL; ?>attend/">
<table>
<tr>
<td>
<input type="text" name="name" id="name" placeholder="Name" value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>">
</td>
</tr>
<tr>
<td>
<input type="text" name="email" id="email" placeholder="Email" value="<?php if (isset($email)) { echo htmlspecialchars($email); } ?>">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="date[]" value="1" class="checkbox"><label>4-5th January</label>
<input type="checkbox" name="date[]" value="2" class="checkbox"><label>11-12th January</label>
<input type="checkbox" name="date[]" value="3" class="checkbox"><label>18-19th January</label>
<input type="checkbox" name="date[]" value="4" class="checkbox"><label>25-26th January</label>
</td>
</tr>
<tr style="display: none;">
<th>
<label for="address">Address</label>
</th>
<td>
<input type="text" name="address" id="address">
<p>Humans: Please leave this field blank.</p>
</td>
</tr>
</table>
<input type="submit" value="I'm in!">
</form>
<?php } ?>
</div>
<?php include (ROOT_PATH . '/inc/footer.php'); ?>
1 Answer
Patrick Metcalfe
Courses Plus Student 7,563 PointsOkay so there are 2 issues that most likely are causing the error after the submit. Both are related to the Header() method you called in the phpmailer section. The header has two weird requirements
- the first is that it must be first: that is, nothing can be outputted before the header() method and you have a configuration file and an error message system that may be outputting something causes the redirect to not function.
- The header() method for Location: URL must follow the form:
header('Location: http://google.com/?foo=bar');
Notice the quotation marks. You haveecho "Location :".BASE_URL ."attend/?status=thanks";
Which yields no quotation marks. Make sure you add or escape another set of quotation marks so they can be interpolated.
Tzevai Chong
15,903 PointsTzevai Chong
15,903 PointsHi Patrick. I've managed to sort it out now. I turned on error reporting on my server and it turned out I was missing a file for the PHPMailer to work! I could kick myself!
Thanks for taking the time to give me some advice.