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

Christiaan Quyn
Christiaan Quyn
14,706 Points

Linking form with Google reCAPTCHA using PHP

Hi Guys,

I'm not really that great with PHP but I just can't seem to figure out how to make this form work when I hit the submit button. What am I doing wrong ? Would greatly appreciate if someone can point me in the right direction, when I click submit - the form does nothing now.

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $secret = '_key_';
    $response = $_POST['g-recaptcha-response'];
    $remoteip = $_SERVER['REMOTE_ADDR'];

    $url = open_https_url("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
    $result = json_decode($url);
    if(!$result->success)
        die('DIRTY ROBOT');

    $from = 'FOX Resorts Contact Page';
    $to = 'christianq010@gmail.com';
    $subject = 'Message from FOX Resorts Website';

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

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }


// If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! We will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }
}
?>

My form on my html

<form action="contact.php" method="post" role="form">
            <div class="row">
                <div class="col-md-10 col-md-push-1 col-sm-12 col-sm-push-0 col-xs-12 col-xs-push-0">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <input class="form-control" id="name" placeholder="Name" type="text"
                                       value="<?php echo htmlspecialchars($_POST['name']); ?>">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <input class="form-control" id="email" placeholder="Email" type="email"
                                       value="<?php echo htmlspecialchars($_POST['email']); ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <textarea name="message" class="form-control" id="" cols="30" rows="7" placeholder="Message">
                                    <?php echo htmlspecialchars($_POST['message']);?>
                                </textarea>
                                <?php echo "<p class='text-danger'>$errMessage</p>";?>
                            </div>
                        </div>
                        <div class="g-recaptcha col-md-6" data-sitekey="6Lf6oBQUAAAAANQ20OznAX9NUIuZwtOVMOJlCvN9">
                        </div>
                        <div class="col-md-12">
                        <br>
                            <div class="form-group">
                                <input value="Send" id="submit" class="btn btn-primary" type="submit" name="submit">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-12">
                                <?php echo $result; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            </form>