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 Post Variables

$_POST is empty

I tried to read from $_POST using php code: var_dump($_POST);

But the result is array(0) { }

Anything I need to set?

6 Answers

You have a typo for the form 'method' attribute. Maybe thats the issue. Let me know.

Thank you

You are welcome :) Happy coding!

This means that the $_POST array did not receive any variables when the form was sent. Did you give name attributes to each for input element? These are the keys which are created within $_POST. Also ensure you're checking $_POST in the corresponding file to where the form's action attribute points to

In my contact.php

        <form mehod="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>

In my contact-process.php

<?php
    var_dump($_POST);
?>

URL after I clicked the Send Button

http://localhost/shirts4mike/contact-process.php?name=Mike&email=mike%40gmail.com&message=hi

Mohamed Nabih
Mohamed Nabih
11,416 Points

I'm still getting empty array

contact.php

                     <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>

contact-process.php

<?php

var_dump($_POST);

?>

Your for and name attributes are missing an equals sign. There is no key being registered in the $_POST array as a result.

<form mehod="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>

whats wrong in my code i am getting empty array :(