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 Checking the Request Method

http://localhost:3030/randy/contact.php?name=&email=&message=

Hello,

This code below is directly related to the project of this class. The if() is checking for 'POST'. I purposely set the form's method to 'get' just to see what would happen.

As I expected the form appeared and I was not redirected to contact-thanks.php; however the address bar shows the below. I believe the if() clause is not suppose to run, so why does the address bar show these variables: name =, email=, message=

I think the variables set in the if () clause should not show up at all in the address bar as long as the if() is not met or satisfied. Can you please tell me where the url get these variables from Thanks

http://localhost:3030/randy/contact.php?name=&email=&message=

Dear ALL:

I believe some changes were made to the Post page that are affecting the the Best Answer option display.

Please make sure to reply to the above post under Add an Answer (it is way at the bottom of this entire page, so please scroll far down to post so that I can click on the best answer option ) section below*

Cheers all!

The code in question:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];


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


?>
<div class="section page">

<div class="wrapper">

    <h1>Contact </h1>
    <p>I&rsquo;s love to hear from you!</p>

    <form method="get" action="contact.php">
        <table>
            <tr>
                <th>
                    <label for="name"> Name: </label>
                </th>
                <td>
                    <input type="text" name="name" id="name"/>
                </td>
            </tr>
            <tr>
                <th>
                    <label for="email"> Email: </label>
                </th>
                <td>
                    <input type="text" name="email" id="email"/>
                </td>
            </tr>

            <tr>    
                <th>
                    <label for="message"> Message: </label>
                </th>
                <td>
                    <textarea name="message"></textarea>
                </td>
            </tr>


       </table>
    <input type="submit" value="Send this">
    </form>


</div>



</div>

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

1 Answer

Henrik Hansen
Henrik Hansen
23,176 Points

Since the form is made to send a GET request, the values of the form will be shown in the url. The values are really set to empty strings, that is $_GET['email'] == "";

Hello Henrik,

http://localhost:3030/randy/contact.php?name=&email=&message=

Can you please help me with concept:

1) Let say, I remove the <?php....?> and just have the html section of the above code. Are you saying that as long as the form's method is get, then all the inputs' with a name=(name or message, email) will be displayed. In this case, the url doesn show anything after the equal sign because the user didnt fill in data(values)

2) Let say the user had filled in the form, I suppse the <?php ?> would not run or the condition would not be satisfied because the if() would be false, right? if($_SERVER["REQUEST_METHOD"] == "POST"){...}

<h1>Contact </h1>
    <p>I&rsquo;s love to hear from you!</p>

    <form method="get" action="contact.php">
        <table>
            <tr>
                <th>
                    <label for="name"> Name: </label>
                </th>
                <td>
                    <input type="text" name="name" id="name"/>
                </td>
            </tr>
            <tr>
                <th>
                    <label for="email"> Email: </label>
                </th>
                <td>
                    <input type="text" name="email" id="email"/>
                </td>
            </tr>

            <tr>    
                <th>
                    <label for="message"> Message: </label>
                </th>
                <td>
                    <textarea name="message"></textarea>
                </td>
            </tr>


       </table>
    <input type="submit" value="Send this">
    </form>








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