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

Brandon Escalante
Brandon Escalante
5,772 Points

PHP: Working with Post Variables. A small problem, hopefully?

Hey, I'm currently on Programming > Build a Simple PHP Application > Adding a Contact Form > Working with Post Variables.

var_dump just isn't working for me, I'm doing something wrong that I can't seem to find.

It's just showing up as one long string, ignoring the white space. It looks exactly the same in the video contact.php. But maybe I'm missing something elsewhere. I checked, but would love another pair of eyes on this, I feel it's something so simple I just can't see.

My PHP:

<?php

var_dump($_POST);
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

echo $name;
echo $email;
echo $message;

?>

On accident I hit the "Submit Post" button twice and posted this posted double. Is there anyway to remove the double post "A small problem, hopefully?" ? Or can an admin please do that?

7 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

The whitespace is there, but the browser ignores it. If you View Source on the page, you should see the output of var_dump formatted more cleanly. Does that work for you?

If so, you can use the HTML <pre> tag to preserve that whitespace, like this:

<pre><?php var_dump($_POST); ?></pre>

Does that solve your problem? Or is there something deeper going on?

Brandon Escalante
Brandon Escalante
5,772 Points

@Randy thanks for the reply, I appreciate it. It turns out it was much simpler than I thought. It's what I get for trying to push through when I was extremely tired, ha.

Approaching it again, fresh this morning, I instantly saw what my problem was. It was var_dump($_POST);. It needed to be removed. That was it! I couldn't believe how long I spent trying to figure it out, when all I needed to do was step back for a few hours. Lesson learned.

Thanks again for the reply.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Ah yes -- that's exactly what var_dump does! I thought you were trying to get that output nicely formatted; I didn't realize you were trying to remove it. :~)

Randy I am hoping to get your thoughts on this. I am working on the same section of the instructional and I have watched the video three times just to make sure that I have everything correct but for some reason when I input the variable $_POST the response I get from the array is array(0) { }

My form is just as you had it so I am not sure what could be the issue.

My form code

        <form methos="post" action="contact-process.php">

            <table>
                <tr>
                    <th>
                        <label for="name">Name</lable>
                    </th>   
                    <td>
                        <input type="text" name="name" id="name">
                    </td>
                </tr>
                <tr>
                    <th>
                        <label for="email">Email</lable>
                    </th>   
                    <td>
                        <input type="text" name="email" id="email">
                    </td>
                </tr>
                <tr>
                    <th>
                        <label for="message">Message</lable>
                    </th>   
                    <td>
                        <textarea name="message" id="message"></textarea>
                    </td>
                </tr>
            </table>
            <input type="submit" value="Send">

        </form> 

Then this is the php I input for the

<?php

var_dump($_POST);

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
echo $name;
echo $email;
echo $message;


?>

I found out the issue, I can't believe I didn't see it before, but it was a simple typo. Finger slipped when I was typing method.

Helen Anderson
Helen Anderson
6,519 Points

Hi all this one is also not working for me.

The error message is: Server error - HTTP Error 500 (Internal Server Error): -The website encountered an error while retrieving http://localhost/shirts4mike/contact-process.php. It may be down for maintenance or configured incorrectly.

It works when

My form in contact.php looks like this:

<form method="post" action="contact-process.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" id="message"></textarea>
                </td>
            </tr>
        </table>
        <input type="submit" value="Send">

    </form>

my contact-process.php looks like this:

<?php

// var_dump($_POST
$name = $_POST('name');
$email = $_POST('email');
$message = $_POST('message');
 echo $name;
 echo $email;
 echo $message;
 ?>

I have tried double quotes, single quotes, removing semi-colons. The strange thing is that var_dump($_POST) works without the semi-colon. But the variable set up does not.

Thanks in advance

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

With the $_POST variable, you have to use square brackets instead of parentheses ... like this:

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
 echo $name;
 echo $email;
 echo $message;
 ?>

Does that help?