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

PHP Contact Form

I am still a beginner in PHP and having some difficulties getting it to send the information back to my email. I am currently using HTML, Javascript, and PHP. Could someone please give me tips or advice on how to get it to send the information?

*****************PHP********************************

<?php

echo "Thank You!" . " -" . "<a href='thankyou.html style='text-decoration:none;"
        . "color: #ff0099;'>Return Home</a>";
//mail string

$name = $_POST['name']."\n";

$phone = $_POST['phone']."\n";

$email = $_POST['email']."\n";

$company = $_POST['company']."\n";

$city = $_POST['city']."\n";

$states = $_POST['states']."\n";

$message = $_POST['message']."\n";

//mail set up
$formcontent ="From: $name \n Phone: $phone \n Email: $email \n Company: $company"
        . "City: $city \n State: $states \n Message: $message";

$recipient = "youremail@yourdomain.com";

$subject = "Contact Form";

$mailheaders = "From: $email \r\n";

$mailheaders .= "Reply - To: ".$_POST["email"];

// Sending mail

mail($recipient, $subject, $formcontent, $mailheaders);
?>

*******************JavaScript**************

(function(){
    var inputs =document.createElement('input');
    var supports = {};

    supports.autofocus = 'autofocus' in inputs;
    supports.required = 'required' in inputs;
    supports.placeholder = 'placeholder' in inputs;

    // Fallback for autofocus attribut
    if(!supports.autofocus) {

    }

    // Fallback for required attribute
    if(!supports.required) {

    }

    // Fallback for placeholder attribute
    if(!supports.placeholder){

    }

    // Change text inside send button on submit
    var send = document.getElementById('contact-submit');
    if(send) {
        send.onclick = function () {
            this.innerHTML = '...Sending';
        };
    };
}) ();

*********************HTML**************

<form action="sendmail.php" method="POST" id="form">
            <p><strong>Name:  (required)</strong><br /> 
                <input type="text" size="25" name="name" placeholder="Name" tabindex="1" required autofocus /></p>
            <p><strong>Call Back Number:  (required)</strong><br />
                <input type="text" size="25" name="phone" placeholder="Phone Nubmer" tabindex="1" required autofocus /></p>
            <p><strong>E-Mail Address:  (required)</strong><br />
                <input type="text" size="25" name="email" placeholder="Email" tabindex="1" required autofocus /></p>
            <p><strong>Company Name:  (required)</strong><br />
            <input type="text" size="25" name="comapany" placeholder="Company Name" tabindex="1" required autofocus/></p>
            <p><strong>City:  (required)</strong><br />
                <input type="text" size="25" name="city" placeholder="City Please" tabindex="1" required autofocus /></p>
            <p><strong>Select Your State: (required)</strong><br />
                    <select name="States[]" required autofocus>
                        <option value="Alabama">Alabama</option>
                        <option value="Alaska">Alaska</option>
                        <option value="Arizona">Arizona</option>
                        <option value="Arkansas">Arkansas</option>
                        <option value="California">California</option>
                        <option value="Colorado">Colorado</option>
                        <option value="Connecticut">Connecticut</option>
                        <option value="Delaware">Delaware</option>
                        <option value="Florida">Florida</option>
                        <option value="Georgia">Georgia</option>
                        <option value="Hawaii">Hawaii</option>
                        <option value="Idaho">Idaho</option>
                        <option value="Illinois">Illinois</option>
                        <option value="Indiana">Indiana</option>
                        <option value="Iowa">Iowa</option>
                        <option value="Kansas">Kansas</option>
                        <option value="Kentucky">Kentucky</option>
                        <option value="Louisiana">Louisiana</option>
                        <option value="Maine">Maine</option>
                        <option value="Maryland">Maryland</option>
                        <option value="Massachusetts">Massachusetts</option>
                        <option value="Michigan">Michigan</option>
                        <option value="Minnesota">Minnesota</option>
                        <option value="Mississippi">Mississippi</option>
                        <option value="Missouri">Missouri</option>
                        <option value="Montana">Montana</option>
                        <option value="Nebraska">Nebraska</option>
                        <option value="Nevada">Nevada</option>
                        <option value="New Hampshire">New Hampshire</option>
                        <option value="New Jersey">New Jersey</option>
                        <option value="New Mexico">New Mexico</option>
                        <option value="New York">New York</option>
                        <option value="North Carolina">North Carolina</option>
                        <option value="North Dakota">North Dakota</option>
                        <option value="Ohio">Ohio</option>
                        <option value="Oklahoma">Oklahoma</option>
                        <option value="Oregon">Oregon</option>
                        <option value="Pennsylvania">Pennsylvania</option>
                        <option value="Rhode Island">Rhode Island</option>
                        <option value="South Carolina">South Carolina</option>
                        <option value="South Dakota">South Dakota</option>
                        <option value="Tennessee">Tennessee</option>
                        <option value="Texas">Texas</option>
                        <option value="Utah">Utah</option>
                        <option value="Vermont">Vermont</option>
                        <option value="Virginia">Virginia</option>
                        <option value="Washington">Washington</option>
                        <option value="West Virginia">West Virginia</option>
                        <option value="Wisconsin">Wisconsin</option>
                        <option value="Wyoming">Wyoming</option>
                </select>
            <p><strong>Message:  (required)</strong><br />
                <textarea name="message" cols="30" rows="5" placeholder="If you are trying to contact me please send me a message"
                              tabindex="5" required></textarea></p>
            <p><input type="submit" value="send" /></p>
        </form>

**********************THANK YOU PAGE*******************

    <h1>Thank You! </h1>

2 Answers

Kevin Korte
Kevin Korte
28,149 Points

Well, there are a couple of moving pieces, why don't you post all of your code and we can see what's going on.

ok give me a minute thank you for the reply

I hope I posted the code right :)

Kevin Korte
Kevin Korte
28,149 Points

I reformatted a bit for you :)

Can you confirm if the form posts correctly, first?

There are really two big steps to this

  1. Does the form post (meaning, do my variables hold the info from the form as expected)?

and if so

  1. Does the email send?

We know number 2 doesn't, so knowing if the form posts correctly will give us direction.

Yes the form does show up on the website correctly. I am able to even select the state and the required works as well. When I actually fill the form out and click submit I get in error in my hosting email but the information shows up in the email as well but there is an error and I am not sure what it would be