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

Konrad Pilch
Konrad Pilch
2,435 Points

How to put input in HTML with PHP

HI,

I need to put the message e.g "Success" to be shown on my website where i show my class , how do i do that?

heres my php code

<?php

    if (isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_subject']) && isset($_POST['contact_text'])){
        $contact_name = $_POST['contact_name'];
        $contact_email = $_POST['contact_email'];
        $contact_subject = $_POST['contact_subject'];
        $contact_text = $_POST['contact_text'];

        if (!empty($contact_name) && !empty($contact_email) && !empty($contact_text)) {

            if(strlen($contact_name)>25 || strlen($contact_email)>50 || strlen($contact_text)>1000){
                echo 'Sorry, max lengh for some fields has been exceeded.';
            } else{
                $to = 'xxx@libero.it';
                $subject = 'Contact form submitted.';
                $body = $contact_name."\n".$contact_text;
                $headers = 'From: '.$contact_email;

                if (@mail($to, $subject, $body, $headers)){
                    echo 'Success';
                }else{
                    echo 'Sorry, an error occurred. Please try again later.';
                }
            }
                foreach( $_POST as $value ){
                if(stripos($value, 'Content-Type:') !== FALSE){
                    echo "There wasa porblem withthe information you entered.";
                    exit;
                }
            }
            if($_POST["address"] !="") {
                echo "Your form submission has an error.";
                exit;
            }


         }else {
            echo 'All fields are required';
         }
}
?>
Hugo Paz
Hugo Paz
15,622 Points

Hi Konrad,

Do you want to show the various messages on a specific page?

Konrad Pilch
Konrad Pilch
2,435 Points

I want to make a message that displays on html e.g <div><?php $message ?></div> so when the user clicks the submit, and the message will get displayed in that div whether is submited or not in a specific place of my webiste. Because if i send my form now, the message will be at the top of the web and will make things ugly . If i put it in other file eg. mail.php , the submit button will re-direct me to a new blank page with the writing of Success or other .

But i want it display where i want : p

Hugo Paz
Hugo Paz
15,622 Points

Staying on the same page that deals with the form you could do something like this:

<?php

    if (isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_subject']) && isset($_POST['contact_text'])){
        $contact_name = $_POST['contact_name'];
        $contact_email = $_POST['contact_email'];
        $contact_subject = $_POST['contact_subject'];
        $contact_text = $_POST['contact_text'];

        if (!empty($contact_name) && !empty($contact_email) && !empty($contact_text)) {

            if(strlen($contact_name)>25 || strlen($contact_email)>50 || strlen($contact_text)>1000){
                echo 'Sorry, max lengh for some fields has been exceeded.';
            } else{
                $to = 'xxx@libero.it';
                $subject = 'Contact form submitted.';
                $body = $contact_name."\n".$contact_text;
                $headers = 'From: '.$contact_email;
                $message = '';

                if (@mail($to, $subject, $body, $headers)){
                    $message = 'Success';
                }else{
                    $message = 'Sorry, an error occurred. Please try again later.';
                }
            }
                foreach( $_POST as $value ){
                if(stripos($value, 'Content-Type:') !== FALSE){
                    $message = "There wasa porblem withthe information you entered.";
                    exit;
                }
            }
            if($_POST["address"] !="") {
                $message = "Your form submission has an error.";
                exit;
            }


         }else {
            $message =  'All fields are required';
         }
//Print the message inside a div with a specific class
echo "<div class='your_class'>" . $message . "</div>";
}
?>
Konrad Pilch
Konrad Pilch
2,435 Points

xd i did similar thing but i did concatinate it wrong and didnt do the exact thing . And yes this is what i mean :D thank you very much , thats really nice and learned new lesson . Thank you!