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
marsha grasett
9,995 Pointscontact form - header()function help
Re Randy's tutorial PROGRAMMING - BUILD A SIMPLE PHP APPLICATION - adding contact form:
I have my contact form on the bottom of my one page scroll site. The contact nav link scrolls down to the contact section.
I don't want to redirect to another page.
I want to stay on index.php
What would I use in place of:
header("Location:contact.php?status=thanks");
in the code below
if($mail->Send()) {
header("Location:contact.php?status=thanks");
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
8 Answers
Casey Ydenberg
15,622 PointsYou don't need to use a header at all. You could store the return $mail->Send() in another variable, and use it to determine what the user will see further down the page.
I think the real question is - what view do you want the user to see when they've successfully sent an email?
Presumably, you don't want to re-display the contact form. You could use an if statement to determine if an email has already been sent, and if so, show something else.
marsha grasett
9,995 PointsAn if statement to determine if an email has already been sent, and if so, show something else.
That is perfect!
Could you point me in a direction to explore this? Thanks!
marsha grasett
9,995 PointsRandy has this inside the contact form:
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php } else { ?>
<?php
if (!isset($error_message)) {
echo '<p>I’d love to hear from you! Complete the form to send me an email.</p>';
} else {
echo '<p class="message">' . $error_message . '</p>';
}
?>
The last two messages work for me. The first message does not work. Presuming because my
header("Location .....
is wrong.
Chad Shores
Courses Plus Student 8,868 PointsHey Marsha, I want to say first that you must be the most patient web designer in the world to have been working with this mail function for this amount of time and have kept a level head about it. That skill is invaluable in life.
Make sure to think about your other options. Sometimes, hiring another developer to work out a custom script, or using third-party web services that provides the functionality for you could be of benefit so that you can spend more time utilizing your strengths which will allow you to excel on a project rather than get stuck. Don't let pride get in the way of completing a project, or your abilities to exceed your clients expectations.
One option is http://www.wufoo.com/ - Online form builder; which I have never used, but have heard good things about.
Just so you know, my big weaknesses are currently JavaScript and jQuery!
Casey Ydenberg
15,622 PointsHi Marsha,
I can't think of a site off the top of my head that would do what you're looking for, and even if I could, we wouldn't be able to see their code.
Here is approximately what you would want to do:
<?php
$err = '';
$sent = FALSE;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//input and validate all the data, storing any problems in $err
//set up the mail script
if ($mail->Send() && $err = '') {
$sent = TRUE;
}
}
?>
<!doctype html>
*rest of the code for your site goes here*
<div id = "contact_form>
<?php
if ($sent) {
?>
<p>Thank you! We will get back to you shortly!</p>
<?php
} else {
echo $err;
?>
<form>Contact form</form>
<?php
}
?>
</body>
</html>
Obviously this would need some tweaking but if you can read through that and understand the logic of it, you can probably figure out how to implement it on your project.
Good luck, Casey
marsha grasett
9,995 Points@Charles - I know :( I just wanted to do it myself with a little (I mean a lot) of help from forums etc. But it is proving way over my head. Thanks for that link.
@Casey thanks for that start. I will try until the end of the week Hopefully I can remain sane until then. After that, if I can't resolve the code, I will just have an email contact and no form.
Nikolaos Papathanassopoulos
10,322 Pointsif you're using the header (location: ) function in php always make sure that no HTML gets displayed before your header relocation.
also what randy did isn't quite right, (but still works most of the time): always use an absolute path in the header relocation. (if you're following randys tutorial you will do that via a php constant "BASE_URL")
to redirect to the same page just use the absolute path:
if($mail->Send()) {
header("Location: http://www.yourdomain.com/yoursubfolder/index.php?status=thanks");
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
Hope this helps. It did for me. ~ Nikos.
adrie silva
6,416 PointsHi Nikos!
What would you suggest if I'm using my contact form with an iframe?
Thank you.
marsha grasett
9,995 PointsI am doomed. All for naught. I might just cry right now
Randy posted this in my previous thread:
"I think for this you don't actually want to submit the form and navigate away from the page. For something like this, you would want to use JavaScript to submit the form information behind the scenes with an Ajax request so that it could leave the user on the same page. This is pretty complicated, though, because you'd have to write a PHP web service that could receive such an Ajax request and then handle all the processing on the server.
I'm hoping to cover something like this (building a simple PHP web service) in my next project."