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 Build a Simple PHP Application Adding a Contact Form Working with Get Variables

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

Why Can't I echo the message??

I have followed the teacher all way, and I want to improve the contact form whenever the user sends the contact form, the page will display the preview of the message and some thank you note. So I use to echo $body_message but it won't work, I try to use $_POST[] method also it won't work. Here are the codes:

<?php 
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $text = $_POST['message'];
    $body_message = '';
    $body_message = 'Name: '. $name. '\n';
    $body_message = 'Email: '. $email. '\n';
    $body_message = 'Message: '. $text;

    header('Location: contact.php?status=thanks');
    exit;
    }
?>
    <?php 
    $pageTitle = 'Contact Mike';
    $underLine = 'contact';
    include('inc/header.php');
?>

    <div class='section page'>
        <div class='wrapper'>

            <h1>Contact</h1>
            <?php if(isset($_GET['status']) AND $_GET['status'] == 'thanks') {?>
            <h2 style='text-align: center'>Preview</h2>
                <p><?php echo $body_message; ?></p>
                <p>Thanks for contacting Mike, he will be in touch shortly!</p>
            <?php } else { ?>

                <p>I'd be very happy to hear message from you</p>
                <form method='post' action='contact.php'>

                    <table>
                        <tr>
                            <th>
                                <label for='name'>Name</label>
                            </th>
                            <td>
                                <input type='text' name='name' id='name' placeholder='Type your name'>
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for='email'>Email</label>
                            </th>
                            <td>
                                <input type='email' name='email' id='email' placeholder='Type your email'>
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for='message'>Message</label>
                            </th>
                            <td>
                                <textarea name='message' id='message' placeholder='Type your message'></textarea>
                            </td>
                        </tr>
                    </table>
                    <input type='submit' value='send'>
                </form>
            <?php } ?>
        </div>

    </div>

<?php include('inc/footer.php');?>

I think the $_POST method is no more exist in the page. Am I right? how we access the user input ?? so that I can preview the message in the page. Thanks :)

Updated spelling Have a nice day :-)

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

lol sorry for my bad english hehehe :)

No problem buddy :)

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

but why it use double "I" ??? o.O

Oh haha sorry about that!

Stipe Stipic
Stipe Stipic
17,520 Points

Hi, The problem is When you submit the Form you go into the if statement: if ($_SERVER['REQUEST_METHOD'] == 'POST') { ... } in the if statement you have the: $_POST['name'] , $_POST['email']... values, but after that you make a GET request to the location contact.php: header('Location: contact.php?status=thanks'); now you have the $_GET['status'] value. But no $_POST[] values anymore. But you can send the varible with the url.

example:

if ($_SERVER['REQUEST_METHOD'] == 'POST') { $name = $_POST['name'];

header('Location: contact.php?status=thanks&name= " . $name . "');
exit;

}

And now you can take the $_GET['name'] to take the name that you have postet.

Another solution is that you store your $_POST[] variables into Session variables in the if statement: like this (but first you must turn on sessions on the top of the page onli put <?php session_start(); ?>):

if ($_SERVER['REQUEST_METHOD'] == 'POST') { $_SESSION['name'] = $_POST['name']; $_SESSION['email'] = $_POST['email']; header('Location: contact.php?status=thanks'); exit; }

//And now you can use $_SESSION['name'], $_SESSION['email']...

I hope I could help. Sorry about my English :)

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

Thanks stipe it works :)

1 Answer

Vitaliy Bogdanov
Vitaliy Bogdanov
7,612 Points

Maybe your problem is here:

$body_message = '';
    $body_message = 'Name: '. $name. '\n';
    $body_message = 'Email: '. $email. '\n';
    $body_message = 'Message: '. $text;

In this code $body_message contain only 'Message: '. $text;

You need concationation operator like this:

$body_message = '';
    $body_message .= 'Name: '. $name. '\n';
    $body_message .= 'Email: '. $email. '\n';
    $body_message .= 'Message: '. $text;
Rifqi Fahmi
Rifqi Fahmi
23,164 Points

thats not solve my problem, but you right I forgot to type '.' to concatenate that message. Thanks :)