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 trialAndreas Poulsen
3,430 PointsContact Form Not Working!
It looks like it is all working. I receive the Email but it's like it doesn't send the information/variables.
HTML - the form code.: <form action="help/form_process.php" method="post"> <header> <h2>Spørgsmål? Bestilling?</h2> <p>Udfyld formen her under, så kontakter jeg dig hurtigts muligt!</p> </header>
<?php
$s = $_GET['s'];
if ($s=="1") {
echo ('<span>Tak for din besked! Jeg vil svare tilbage indenfor 24 timer!</span>');
}
?><br>
<label>Navn</label>
<input type="text" name="name" id"name" placeholder="Fornavn Efternavn" width="100px;">
<label>Email</label>
<input type="text" name="email" id="email" placeholder="Email@email.com">
<label>Telefon nummer</label>
<input type="text" name="contact" id="contact" placeholder="20415069">
<label>Hjemmeside URL</label>
<input type="text" name="website" id="website" placeholder="www.dinside.dk">
<label>Besked</label>
<textarea name="message" rows="10" cols="15" id="message" placeholder="besked"></textarea>
<input type="submit" value="Send Besked" id="submit">
</form>
The PHP sending file!: <?php
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$website = $_POST['website'];
$message = $_POST['message'];
$to = "support@lyngholmwebs.dk";
$subject = "Ny Besked!";
$message = "Ny besked fra LyngholmWebs.dk kontakt form! \n\n Name: $name \n\n Email: $email \n\n Telefonnummer: $contact \n\n Website: $website \n\n Besked: $message \n\n";
mail($to, $subject, $message);
header("Location:../kontakt.php?s=1");
?>
PLEASE HELP!
22 Answers
Philip G
14,600 PointsI set up an test environment for you at:
http://www.sznews.at/lyngholmwebs/
You find the executing code and .phpcode with the code(Sending to your E-Mail).
Philip G
14,600 PointsHi Andreas,
Where do you open the <form> tag?
You forgot the equal sign in id*=*"name"
Try this to send the mail:
<?php
$header = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $header);
?>
Logan R
22,989 PointsYou need to wrap your contact form in the < form > element.
<form action="form.php" method="post">
<fieldset>
<label>Navn</label>
<input type="text" name="name" id"name" placeholder="Fornavn Efternavn" width="100px;">
<label>Email</label>
<input type="text" name="email" id="email" placeholder="Email@email.com">
<label>Telefon nummer</label>
<input type="text" name="contact" id="contact" placeholder="20415069">
<label>Hjemmeside URL</label>
<input type="text" name="website" id="website" placeholder="www.dinside.dk">
<label>Besked</label>
<textarea name="message" rows="10" cols="15" id="message" placeholder="besked"></textarea>
</fieldset>
<input type="submit" value="Send Besked" id="submit">
</form>
Also, very side note: After header you need to include
exit();
The exit stops the code from reading the rest of the file, which can lead to headaches.
Mike Fondario
11,286 PointsDirectly under your <label>Navn</label> I noticed an id has an error it's "id"name".
Shawn Flanigan
Courses Plus Student 15,815 PointsAndreas,
Is there a reason you're not wrapping your form in <form>
tags? Did you just omit them for the question? It might help to diagnose the problem if we could see those tags. Also, in your first input (name), you're missing an =
between id
and "name"
.
Philip G
14,600 PointsIs the form data in chrome devtools under network tab correct?
Andreas Poulsen
3,430 PointsI tried everything you guys said.
It's sending the Email, but it doesn't send the information, this is what the Email looks like:
Ny besked fra LyngholmWebs.dk kontakt form!
Name:
Email:
Telefonnummer:
Website:
Besked:
With no information
Andreas Poulsen
3,430 Points< form action="form.php" method="post" > <fieldset> <label>Navn</label> <input type="text" name="name" id="name" placeholder="Fornavn Efternavn" width="100px;">
<label>Email</label>
<input type="text" name="email" id="email" placeholder="Email@email.com">
<label>Telefon nummer</label>
<input type="text" name="contact" id="contact" placeholder="20415069">
<label>Hjemmeside URL</label>
<input type="text" name="website" id="website" placeholder="www.dinside.dk">
<label>Besked</label>
<textarea name="message" rows="10" cols="15" id="message" placeholder="besked"></textarea>
</fieldset>
<input type="submit" value="Send Besked" id="submit">
</ form >
This is my form, and I do have the < form > tags
Logan R
22,989 PointsNope, never mind... This answer didn't work either when I tried it >.>
Andreas Poulsen
3,430 PointsNope, I tried it aswell
Andreas Poulsen
3,430 PointsAm I allowed to post the Website URL so you can see the code?
Philip G
14,600 PointsYes please!
Andreas Poulsen
3,430 PointsLyngholmWebs.dk/kontakt
I really don't know what's up with this....
Andreas Poulsen
3,430 PointsI got this from your form: Ny besked fra LyngholmWebs.dk kontakt form!
Name:
Email:
Telefonnummer:
Website:
Besked:
Logan R
22,989 PointsI tested your code on my own webserver and it would not allow me to send an email until I changed it to:
mail($to, $subject, $message, "From: support@example.com");
I don't think the header is your problem because you are getting an email. The email I received looked like this:
Ny besked fra LyngholmWebs.dk kontakt form!
Name: asdf1
Email: asdf2
Telefonnummer: asdf3
Website: asdf4
Besked: asdf5
Andreas Poulsen
3,430 PointsLogan R, can you send me the whole code? It doesn't work for me
Shawn Flanigan
Courses Plus Student 15,815 PointsThe only other thing I see that could be causing problems is your width="100px;"
in your name
input. Try style="width: 100px;"
instead. Or width="30"
.
Andreas Poulsen
3,430 Pointsdidn't work either... :(
Philip G
14,600 PointsI'm working on your problem.
Andreas Poulsen
3,430 PointsThanks, Philip!
Shawn Flanigan
Courses Plus Student 15,815 PointsHmm...have you tried breaking apart your message like this?
$message = "Ny besked fra LyngholmWebs.dk kontakt form! \n\n Name: ";
$message .= $name;
$message .= " \n\n Email: ";
$message .= $email;
$message .= " \n\n Telefonnummer: ";
$message .= $contact;
etc.
Andreas Poulsen
3,430 PointsYes, didn't work...
Andreas Poulsen
3,430 PointsI receive the Email, but the information gets lost...
PHP Code: <?php
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$website = $_POST['website'];
$message = $_POST['message'];
$to = "support@lyngholmwebs.dk";
$subject = "Ny Besked!";
$message = "Ny besked fra LyngholmWebs.dk kontakt form! \n\n Name: $name \n\n Email: $email \n\n Telefonnummer: $contact \n\n Website: $website \n\n Besked: $message \n\n";
mail($to, $subject, $message, "From: support@example.com");
header("Location:../kontakt.php?s=1");
?>
And this is the Email I get:
Ny besked fra LyngholmWebs.dk kontakt form!
Name:
Email:
Telefonnummer:
Website:
Besked:
Philip G
14,600 PointsThis WORKS for me!!:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$website = $_POST['website'];
$message = $_POST['message'];
$to = "support@lyngholmwebs.dk";
$subject = "Ny Besked!";
$message = "Ny besked fra LyngholmWebs.dk kontakt form! \n\n Name: ".$name." \n\n Email: ".$email." \n\n Telefonnummer: ".$contact." \n\n Website: ".$website". \n\n Besked: ".$message." \n\n";
mail($to, $subject, $message, "From: support@example.com");
header("Location:../kontakt.php?s=1");
?>
Did it also work for you?:)
Andreas Poulsen
3,430 PointsI got an error: Parse error: syntax error, unexpected '". \n\n Besked: "' (T_CONSTANT_ENCAPSED_STRING) in /home/lynghol1/public_html/help/form_process.php on line 11
Andreas Poulsen
3,430 PointsIt doesn't work: <?php
$name = $_POST['name']; $email = $_POST['email']; $contact = $_POST['contact']; $website = $_POST['website']; $message = $_POST['message'];
$to = "support@lyngholmwebs.dk"; $subject = "Ny Besked!"; $message = "Ny besked fra LyngholmWebs.dk kontakt form! \n\n Name: ".$name." \n\n Email: ".$email." \n\n Telefonnummer: ".$contact." \n\n Website: ".$website." \n\n Besked: ".$message." \n\n";
mail($to, $subject, $message, "From: support@example.com");
header("Location:../kontakt.php?s=1");
?>
Shawn Flanigan
Courses Plus Student 15,815 PointsOne small error:
$message = "Ny besked fra LyngholmWebs.dk kontakt form! \n\n Name: ".$name." \n\n Email: ".$email." \n\n Telefonnummer: ".$contact." \n\n Website: ".$website." \n\n Besked: ".$message." \n\n";
Philip G
14,600 PointsDelete the single Quote before .$message. Be sure to copy my Code exactly.
Andreas Poulsen
3,430 PointsIt still doesn't send the information...
Andreas Poulsen
3,430 PointsCan you send the form too? I feel like I messed something up, trying to fix it
Andreas Poulsen
3,430 PointsPhilip, I copied your code. What HTML code do you have?
This is my code: < form action =" post.php " method="post" > < fieldset> < label>Navn</label> < input type="text" name="name" id="name" placeholder="Fornavn Efternavn" width="100px;">
< label>Email</label>
< input type="text" name="email" id="email" placeholder="Email@email.com">
<label>Telefon nummer</label>
<input type="text" name="contact" id="contact" placeholder="20415069">
<label>Hjemmeside URL</label>
<input type="text" name="website" id="website" placeholder="www.dinside.dk">
<label>Besked</label>
<textarea name="message" rows="10" cols="15" id="message" placeholder="besked"></textarea>
</fieldset>
<input type="submit" value="Send Besked" id="submit">
</form>
Shawn Flanigan
Courses Plus Student 15,815 PointsJust a sidenote...you know that "post.php" isn't the name of the php file on your server, right?
Andreas Poulsen
3,430 PointsI know, it's not letting me post, if I use the real name: < form action="help/form_process.php" method="post" >
Andreas Poulsen
3,430 PointsI really don't know what the problem is... Can someone please help?
Andreas Poulsen
3,430 PointsI give up on this...
Jeff Busch
19,287 PointsDon't do that.
Andreas Poulsen
3,430 PointsI have no clue what's wrong, and nobody else does. I tried everything.
Shawn Flanigan
Courses Plus Student 15,815 PointsIs there more code in your form_process.php file or is it just this?
Andreas Poulsen
3,430 PointsThis is the whole script: <?php
$name = $_POST['name']; $email = $_POST['email']; $contact = $_POST['contact']; $website = $_POST['website']; $message = $_POST['message'];
$to = "support@lyngholmwebs.dk"; $subject = "Ny Besked!"; $message = "Ny besked fra LyngholmWebs.dk kontakt form! \n\n Name: ".$name." \n\n Email: ".$email." \n\n Telefonnummer: ".$contact." \n\n Website: ".$website." \n\n Besked: ".$message." \n\n";
mail($to, $subject, $message, "From: support@example.com");
header("Location:../kontakt.php?s=1");
?>
And it doesn't work.. It sends the Email, but not the variables
Here is the whole FORM: !* <?php $titleName = "Kontakt - LyngholmWebs.dk"; include('help/headerDK.php'); ?> <div id="content" class="grid_12 omega"> <div id="envelope">
<form action="post.php" method="post">
<header>
<h2>Spørgsmål? Bestilling?</h2>
<p>Udfyld formen her under, så kontakter jeg dig hurtigts muligt!</p>
<?php
$s = $_GET['s'];
if ($s=="1") {
echo ('<span>Tak for din besked! Jeg vil svare tilbage indenfor 24 timer!</span>');
}
?>
</header>
<fieldset>
<label>Navn</label>
<input type="text" name="name" id="name" placeholder="Fornavn Efternavn" width="100px;">
<label>Email</label>
<input type="text" name="email" id="email" placeholder="Email@email.com">
<label>Telefon nummer</label>
<input type="text" name="contact" id="contact" placeholder="20415069">
<label>Hjemmeside URL</label>
<input type="text" name="website" id="website" placeholder="www.dinside.dk">
<label>Besked</label>
<textarea name="message" rows="10" cols="15" id="message" placeholder="besked"></textarea>
</fieldset>
<input type="submit" value="Send Besked" id="submit">
</form>
</div>
</div> <div id="line2" class="grid_12 omega"></div> <?php include('help/footerDK.php'); ?> *!
I renamed the script to post.php
Shawn Flanigan
Courses Plus Student 15,815 PointsWeird...I don't know what to tell you. I've tested your form and php code on my localhost and everything is still working fine. Not sure why the variables aren't populating.
Andreas Poulsen
3,430 PointsCan it be my host? Maybe my host won't let me send the information in the email. but that doesn't make sense...
Jeff Busch
19,287 PointsHi Andreas,
Below is a message I received in my email from your form:
Ny besked fra LyngholmWebs.dk kontakt form!
Name: Jeff Busch
Email: jeffreybbusch@gmail.com
Telefonnummer: xxxxxxxxxx
Website: rosebusch.net
Besked: testing 4
<form action ="help/form_process.php" method="post">
<fieldset>
<label>Navn</label>
<input type="text" name="name" id="name" placeholder="Fornavn Efternavn" width="100px;">
<label>Email</label>
<input type="text" name="email" id="email" placeholder="Email@email.com">
<label>Telefon nummer</label>
<input type="text" name="contact" id="contact" placeholder="20415069">
<label>Hjemmeside URL</label>
<input type="text" name="website" id="website" placeholder="www.dinside.dk">
<label>Besked</label>
<textarea name="message" rows="10" cols="15" id="message" placeholder="besked"></textarea>
</fieldset>
<input type="submit" value="Send Besked" id="submit">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$website = $_POST['website'];
$message = $_POST['message'];
$to = "jeffreybbusch@gmail.com";
$subject = "Ny Besked!";
$message2 = "Ny besked fra LyngholmWebs.dk kontakt form! \n\n Name: " . $name . " \n\n Email: ". $email ." \n\n Telefonnummer: " . $contact . " \n\n Website: " . $website ." \n\n Besked: " . $message . " \n\n";
// mail($to, $subject, $message2, "From: support@example.com");
if(mail($to, $subject, $message2, "From: name@example.com")){
echo "The following message sent successfully to " . $to . " with the subject of " . $subject . ": " . $message2;
} else { echo "There was a problem."; }
// header("Location:../kontakt.php?s=1");
?>
Where I have changed $message to $message2 I found a quotation mark in the wrong place also. I really don't know if I needed to comment the header statement but I did because it was of no use for me. I hope this works for you.
Jeff
Andreas Poulsen
3,430 PointsI don't know if I'm allowed to, but can you please help me on some kind of control thing, teamviewer or whatever. Something is wrong... please
Jeff Busch
19,287 PointsIf I can help I will, but I don't know what teamweaver or whatever is. Can you be more specific? Did the code I provided work?
Shawn Flanigan
Courses Plus Student 15,815 PointsAndreas,
I just read something about how some servers won't let you use $_POST
variables in pages with only php. Maybe try adding an HTML doctype to the top of the page (and maybe a bit of content that nobody will ever see?). It's a long shot, but nothing else seems to be working.
Another long shot...maybe your help
folder isn't allowing $_POST
variables. You could try moving your form_process.php
file to the same directory as the form itself?
This is a good mystery. By far the longest thread I've seen on Treehouse recently.
Jeff Busch
19,287 PointsHi Shawn,
Awhile back I help someone with a simple php email form much like this. After some fixing it worked fine from my hosting service but not from her's. I don't know what they had to do on her end; but configuring Apache on a commercial server was beyond my knowledge.
Jeff
Andreas Poulsen
3,430 PointsI tried to move the for_process.php file to the same directory, but with no luck. I also added some HTML and also with no luck...
Andreas Poulsen
3,430 PointsI am in school at the moment, I'll email you later
Shawn Flanigan
Courses Plus Student 15,815 PointsI think I got it! I think the problem is that you're naming your email message $message
and you have a form variable called $message
too. When you try to add your form's message to your email message, it's causing trouble. Rename the form's $message
to something like $note
and see if that helps.
Andreas Poulsen
3,430 PointsNo, it doesn't work... I don't get this... why is this so hard ;)?
Shawn Flanigan
Courses Plus Student 15,815 PointsI don't know. I ran the following to try it out...and everything works fine here. I did also just notice that you forgot to close your first <label>
tag.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$website = $_POST['website'];
$message = $_POST['message'];
$to = "support@lyngholmwebs.dk";
$subject = "Ny Besked!";
$message = "Ny besked fra LyngholmWebs.dk kontakt form! \n\n Name: $name \n\n Email: $email \n\n Telefonnummer: $contact \n\n Website: $website \n\n Besked: $message \n\n";
if(mail($to, $subject, $message, "From: name@example.com")){
echo "The following message sent successfully to " . $to . " with the subject of " . $subject . ": " . $message;
} else { echo "There was a problem."; }
?>
<!DOCTYPE html>
<html>
<form action ="post.php" method="post">
<fieldset>
<label>Navn</label>
<input type="text" name="name" id="name" placeholder="Fornavn Efternavn">
<label>Email</label>
<input type="text" name="email" id="email" placeholder="Email@email.com">
<label>Telefon nummer</label>
<input type="text" name="contact" id="contact" placeholder="20415069">
<label>Hjemmeside URL</label>
<input type="text" name="website" id="website" placeholder="www.dinside.dk">
<label>Besked</label>
<textarea name="message" rows="10" cols="15" id="message" placeholder="besked"></textarea>
</fieldset>
<input type="submit" value="Send Besked" id="submit">
</form>
</html>
If this is working for you, you'll just have to replace the echo
statement with your redirect.
Andreas Poulsen
3,430 PointsIt doesn't work for me. This is what I get with the Echo: The following message sent successfully to support@lyngholmwebs.dk with the subject of Ny Besked!: Ny besked fra LyngholmWebs.dk kontakt form! Name: Email: Telefonnummer: Website: Besked:
Philip G
14,600 PointsPhilip G
14,600 PointsOops, It was sending to my E-Mail. I fixed it.
Andreas Poulsen
3,430 PointsAndreas Poulsen
3,430 PointsIt doesn't work on my website... Do you maybe have an Email so I can so you or something?
Andreas Poulsen
3,430 PointsAndreas Poulsen
3,430 PointsI sent you an email.
Philip G
14,600 PointsPhilip G
14,600 PointsAre you ready?