Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Adding a Contact Form Working with Get Variables

we should not be able to submit an empty form :)

Why we are able to submit an empty form .. Just press submit without entering any data in the three fields , and the thank you message appears.

I think this is not the best case.


3 Answers

Kevin Korte
Kevin Korte
28,111 Points

When the form gets submitted, the first thing you'd want to do is check if the value of the forms are set using something like this isset($_POST['name']) that would check if a name attribute was sent via post. If any of those isset() checks fail, you can redirect back to the post and display an error.

Not sure if the video has showed that yet, but I believe it eventually does.

12,151 Points

So exactly as I have explained in detail in my answer above lol. It seems he's not responding anymore.

12,151 Points

html can not see if the form is filled or not. It simply passes all data of the form to wherever it is send. ( to itself if left blank )

However since html5 came it now is possible to add attributes "required" to form elements which check if certain element like a radio bullet is clicked before sending. There are good and bad things about it. As example the error messages are not styleable or editable in any way.

Hope this answers your question,



That is not possible. PHP cannot directly interfere with the front end. I think what you meant is; how can you send the data back to the form with an error message that its not filled out properly yet, right?

The way I would do it would be like this.

First make an error element in your html where the form resides.

<div class="error_wrapper">
    if(isset($_GET['error'])) {
        $error = $_GET["error"];

        if ($error == "1") {
            echo "<p class=\"error\">Error, form was not filled out properly.</p>";
        //TODO error 2 ??
        if ($error == "2") {
            echo "<p class=\"error\">Unknown error.</p>";

Then in your php where you process the form check if form is filled, if not send it back to the form page with an error number;

header("Location: ../index.php?error=1");

thanks buddy, but i prefer a PHP fix rather than the html required attribute. Regards

12,151 Points

I have edited my comment. Is this what you were looking for?