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

Tom Philpotts
Tom Philpotts
167 Points

Contact form Foundation Zurb

This is my first time using foundation for a build, previously I have used bootstrap.

I have the contact form working and send the emails correctly.

But it is redirecting to another page, how do I get it to simple say thank at the bottom of the form?

Tom Philpotts
Tom Philpotts
167 Points

the code I am using is as follows:

<?php

// configure
$from = 'test@test.com'; 
$sendTo = 'test@test.com';
$subject = 'New message from test';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

// let's do the sending

try
{
    $emailText = "You have new message from contact form\n=============================\n";

    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    mail($sendTo, $subject, $emailText, "From: " . $from);

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}
Tom Philpotts
Tom Philpotts
167 Points

This is the form

              <form id="ajax-contact" method="post" action="mailer.php">
                      <div class="field">
                        <input type="text" name="name" placeholder="first name" required></input>

                      </div>
                      <div class="field">
                        <input type="text" name="surname" placeholder="last name"></input>
                      </div>
                      <div class="field">
                        <input type="text" name="phone" placeholder="telephone" required></input>

                      </div>
                      <div class="field">
                        <input type="email" name="email" placeholder="email" required></input>

                      </div>
                      <div class="field">
                        <textarea type="message" name="message" placeholder="message" required></textarea>

                      </div>
                      <div class="field">
                        <button type="submit">send</button>
                      </div>

                  </form>
                  <div class="form-messages"></div>
Tom Philpotts
Tom Philpotts
167 Points

I was following the AJAX tutorial from Treehouse (http://blog.teamtreehouse.com/create-ajax-contact-form), but could not add in more fields, so took the PHP from another site I built.

I got the AJAX form from the tutorial to work, but it redirects to another page.

I am still a beginner with PHP and Javascript. I know I could add the toggle class on the 'submit' button, however, how would I make sure this was only done on success?

Thank you in advance.

The website is: atonestyling.com.