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

quick php help in php contact !

Hello, I would like customize my php contact form to display message undearneath my submit button saying your message has been received, etc, and also and if statment when the fields are not filled out to ask the person to fill them out correctly but I am stuck, any help?

<?php $name = $_POST['name'];
$email = $_POST['email']; 
$subject = $_POST['subject']; 
$message = $_POST['message'];  
$formcontent="From: $name \n Message: $message";   
$recipient = "fmzpol@gmail.com"; 
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>

10 Answers

Ye try this:

<?php
$name = $_POST['name'];
$email = $_POST['email']; 
$subject = $_POST['subject']; 
$message = $_POST['message'];
if((!$name) || (!$email) || (!$subject) || (!$message)) {
$output= "Please fill in all the fields!";
}else {
$formcontent="From: $name \n Message: $message";   
$recipient = "fmzpol@gmail.com"; 
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)) {
    $output= "Thank you, your message has been recieved!";
}else {
    $output = "Sorry, there was an error sending your message!";
}
}
?>

You have to echo the output message in your html:

<form action="..." method="post">
<!--inputs go here-->
</form>
<?php echo $output; ?>

Thank you so much, but I am still doing something wrong...

So I am suppose to insert the php contact scrip just above the form, and after the form output the <?php echo $output; ?>

Ye that's right, I usually put all my PHP at the top of my code.

We are close Joe, but something is off ;(

If you could take a look please, http://t0ms0nsdesign.com/template

http://pastebin.com/2vXNKe7U - and here is the code , I cant find the mistake :(

I think I am still doing something wrong, check it out if you can : http://t0ms0nsdesign.com, nothing happens, like the code does not load.

here is the code, http://pastebin.com/x5VPqy4k

Like the script loads up before the submit is hit?

Try putting the php code at the very top of your code(just above your doctype). Also could you tell me what the problem is. Thanks

I did, same thing.

The

Please fill in all the fields! is already showing when the page loads up, and the email is not being send, its like it is just not working?

Alright, I looked at your code, this should fix the message showing up before the submit button is clicked:

<?php
if($_POST['submit']) {
$name = $_POST['name'];
$email = $_POST['email']; 
$subject = $_POST['subject']; 
$message = $_POST['message'];
if((!$name) || (!$email) || (!$subject) || (!$message)) {
$output= "Please fill in all the fields!";
}else {
$formcontent="From: $name \n Message: $message";   
$recipient = "fmzpol@gmail.com"; 
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)) {
$output= "Thank you, your message has been recieved!";
}else {
    $output = "Sorry, there was an error sending your message!";
}
}
}else {
    $output = "";
}
?>

No, it gives me a blank white page

I fixed it, Joe!!! Thank you so much , its working now,! THNAKS!

one quick question my here,

after the submit button is hit, either if it sends the message or it does not, the page jumps back to the top and then you have to scroll back to see if it was send, is there an option that the page comes back to the CONTACT part after the button send it hit?

http://t0ms0nsdesign.com/template/index.php

OK, first of all you need to add an id to your form:

<form id="contact" action="index.php" method="post">

</form>

Secondly, you need to change the action attribute to "index.php#contact". Hope this helped!

Joe, thank you so much for all the help!

It seems you know a lot, how did you learn all this & how long it took you?

I have been learning a bit before treehouse and I have been at treehouse for about a month, but I'm only 13.

O wow, I am shocked!

Good luck to you man!

Thanks!