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 Help!!

I've had a lot of trouble understanding how to make a contact form work. Is there any apps such as the ones on Go Daddy that can be edited to fit my needs?

How can I add a new field called phone number into this php?

<?php // VALUES FROM THE FORM $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $message = $_POST['msg'];

// ERROR & SECURITY CHECKS
if ( ( !$email ) ||
     ( strlen($_POST['email']) > 200 ) ||
     ( !preg_match("#^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$#", $email) )
   ) 
{ 
    print "Error: Invalid E-Mail Address"; 
    exit; 
} 
if ( ( !$name ) ||
     ( strlen($name) > 100 ) ||
     ( preg_match("/[:=@\<\>]/", $name) ) 
   )
{ 
    print "Error: Invalid Name"; 
    exit; 
} 
if ( preg_match("#cc:#i", $message, $matches) )
{ 
    print "Error: Found Invalid Header Field"; 
    exit; 
} 
if ( !$message )
{
    print "Error: No Message"; 
    exit; 
} 
if (eregi("\r",$email) || eregi("\n",$email)){ 
    print "Error: Invalid E-Mail Address"; 
    exit; 
} 
if (FALSE) { 
    print "Error: You cannot send to an email address on the same domain."; 
    exit; 
} 


// CREATE THE EMAIL
$headers    = "Content-Type: text/plain; charset=iso-8859-1\n";
$headers    .= "From: $name <$email>\n";
$recipient  = "steinchristensen@hotmail.com";
$subject    = "Contact From Your Website";
$message    = wordwrap($message, 1024);

// SEND THE EMAIL TO YOU
mail($recipient, $subject, $message, $headers);

// REDIRECT TO THE THANKS PAGE
header("location: thanks.php");

?>

Thanks for your help, php is a new area to me!

1 Answer

Hi Stein Christensen,

Looking at your code, you have to take into consideration that you would also need to add a phone number field into that. After doing that, you have to add a variable that would receive that information, just like you have for the other information at the top.

If I were you, I would actually customize the message you are sent. You can use the variables that you have collected and include them in the e-mail you are being sent.

Here is an example from stackoverflow.com that explains in detail and gives you an example.

If that still doesn't answer your question, contact me on Skype. live:lloanalas - I'll be more than happy to explain it in detail currently on break from class