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

Erik Nuber
Erik Nuber
20,629 Points

Where or How to include a snippet of PHP code.

I do not know anything about PHP but, have created a contact page and, found some code I modified that I believe will work to send when the submit button is pressed.

I am not sure what to do with the $from variable. Not sure if I can declare or something similar

$from = 'From' + $email;

Here is the full PHP code

<?php
    $name = $_POST['name'];
    $email = $_POST['mail'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $verify = $_POST['mathProblem'];
    $from = 'From: My Contact Page'; 
    $to = 'erik.nuber@yahoo.com'; 
    $subject = 'New Contact Information';

    $body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";

    if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($verify == '3') {                
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $verify != '3') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
?>

I don't know where to put this though or how to make it actually run.

Here is the form it will work with...

<form action="mail.php" method="post">

                <div id="smamTitleBox">
                    <h1>Send Me A Message</h1>
                </div>    

                <fieldset>
                    <legend> Your Contact Info: </legend>

                    <label class="textAlignmentSingle" for="name">Name:</label>
                    <input type="text" id="name" name="user_name" required>

                    <label class="textAlignmentSingle" for="mail">Email:</label>
                    <input type="email" id="mail" name="user_email" required>

                    <label class="textAlignmentDouble" for="phone">Phone Number:</label>
                    <input type="tel" id="phone" name="user_phone">
                </fieldset>    

                <fieldset>
                    <label class="textAlignmentSingle" for="message">Message:</label>
                    <textarea id="message" name="user_message"></textarea>
                </fieldset>

                <fieldset>
                    <legend>Verification: </legend>
                    <label class="textAlignmentSingle" for="mathProblem">1+2=</label>
                    <input type="number" id="mathProblem" name="user_math" placeholder="Answer" required>
                </fieldset>

                <input type="submit" id="submit" name="submit" value="Submit" >

            </form>

Does the php file go into a php document I would call "mail.php", do I include it inside the header of the contact page? Any help would be greatly appreciated.

2 Answers

Kevin Korte
Kevin Korte
28,149 Points

Your php script would go in mail.php, since that is where the form is calling to when it submits. Wherever your form action is pointing, is where your script to handle the form should go.

Just quickly looking at this, I can see you're going to have a problem, this form wont email.

In your form, for example the name input. The html says <input type="text" id="name" name="user_name" required>. But in your php you're trying to get the value by $name = $_POST['name'];, and it's not going to find that. Either in your html name=name or in your php, you should change it to $name = $_POST['user_name']; Doesn't matter, but whatever your do, the name value on the input has to match the $_POST call.

Erik Nuber
Erik Nuber
20,629 Points

Thank You! I was using ID's instead of the name field. Will modify and, that is what I called the file I put my PHP in.

Appreciate the response.

Kevin Korte
Kevin Korte
28,149 Points

Gottcha. PHP will only grab the value of an input through it's name attribute. It'll ignore any class or id attributes. As long as the php script is named mail.php, and it's in the same level directory as this html, it should submit than.