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

PHP

Contact 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

I 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).

Oops, It was sending to my E-Mail. I fixed it.

It doesn't work on my website... Do you maybe have an Email so I can so you or something?

I sent you an email.

Are you ready?

Hi Andreas,

  1. Where do you open the <form> tag?

  2. You forgot the equal sign in id*=*"name"

  3. 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
Logan R
22,989 Points

You 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.

Directly under your <label>Navn</label> I noticed an id has an error it's "id"name".

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

Andreas,

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".

Is the form data in chrome devtools under network tab correct?

I 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

< 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
Logan R
22,989 Points

Nope, never mind... This answer didn't work either when I tried it >.>

Nope, I tried it aswell

Am I allowed to post the Website URL so you can see the code?

Yes please!

LyngholmWebs.dk/kontakt

I really don't know what's up with this....

I got this from your form: Ny besked fra LyngholmWebs.dk kontakt form!

Name:

Email:

Telefonnummer:

Website:

Besked:

Logan R
Logan R
22,989 Points

I 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

Logan R, can you send me the whole code? It doesn't work for me

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

The 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".

didn't work either... :(

I'm working on your problem.

Thanks, Philip!

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

Hmm...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.

Yes, didn't work...

I 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:

This 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?:)

I 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

It 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
Shawn Flanigan
Courses Plus Student 15,815 Points

One 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";

Delete the single Quote before .$message. Be sure to copy my Code exactly.

It still doesn't send the information...

Can you send the form too? I feel like I messed something up, trying to fix it

Philip, 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
Shawn Flanigan
Courses Plus Student 15,815 Points

Just a sidenote...you know that "post.php" isn't the name of the php file on your server, right?

I know, it's not letting me post, if I use the real name: < form action="help/form_process.php" method="post" >

I really don't know what the problem is... Can someone please help?

I give up on this...

Don't do that.

I have no clue what's wrong, and nobody else does. I tried everything.

Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,815 Points

Is there more code in your form_process.php file or is it just this?

This 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
Shawn Flanigan
Courses Plus Student 15,815 Points

Weird...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.

Can it be my host? Maybe my host won't let me send the information in the email. but that doesn't make sense...

Hi 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

I 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

If 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
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

Andreas,

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.

Hi 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

I 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...

I am in school at the moment, I'll email you later

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

I 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.

No, it doesn't work... I don't get this... why is this so hard ;)?

Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,815 Points

I 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.

It 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: