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

Help for contact form success message

Hello everyone i'm building my personal portfolio and sincerly i don't have any experience for the moment with php . I finished my site the only thing i'm missing is how to display a success message (even in another page) with this code.

        ```php
        <?php
$name = $_POST['name'];
$email = $_POST['email'];
$organisation = $_POST['organisation'];
$brand = $_POST['brand'];
$design = $_POST['design'];
$frontend = $_POST['frontend'];
$backend = $_POST['backend'];
$ecommerce = $_POST['ecommerce'];
$notsure = $_POST['notsure'];
$message = $_POST['message'];
$web1 = $_POST['web1'];
$web2 = $_POST['web2'];
$web3 = $_POST['web3'];
$web4 = $_POST['web4'];
$deadline = $_POST['deadline'];
$reference = $_POST['reference'];
$budget = $_POST['budget'];



$from = "From:". $_POST['email'];
$to = 'mei@gmail.com'; 
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Organisation: $organisation\n Brand-identity: $brand\n Web-  design:  $design\n Front-end: $frontend\n Backend: $backend\n Ecommerce: $ecommerce\n Not Sure: $notsure\n Message:   $message\n Links to stuff : $web1 $web2 $web3 $web4\n Deadline: $deadline\n Reference: $reference\n Budget:   $budget\n";

 $headers = "From:" . $from . "\r\n";
 $headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n"; 
 if (isset($_POST['submit'])) {
 if(@mail($to,$subject,$body,$headers))
{
print "Mail sent !";
}else{
echo "Error! Please try again.";
}
}

      ?>
        ```

The problem is that my sucess message is always displayed and not only when my form is submitted. Can someone help me

3 Answers

John MacDonald
John MacDonald
8,593 Points

Hey Gregorio!

I recommend instead of posting to the wall "Mail Sent!", You could use a php header('location: yourpagehere.php?success'); The benefits of this is that you can have an awaiting styled success message when the person hits the $_GET variable. Example - php <?php if(isset($_GET['success'])){ echo "Success, Mail has been sent!"; }; ?>

Hope this helps!

John \n\n

John MacDonald sorry i sincerly don't understand since I don't have a php knowledge for the moment where I have to place your code in order to don't have my success message always displayed

John MacDonald
John MacDonald
8,593 Points

Hmmm, I recommend checking out Treehouses PHP videos, They will give you quite a good understanding with PHP and you will be done in about a month or 2.

Hope this helps!

John /n/n