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

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.

Regards

3 Answers

Kevin Korte
Kevin Korte
28,149 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.

shadryck
shadryck
12,151 Points

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

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

shadryck

//EDIT

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">
    <?php
    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>";
        }
    }
    ?>
</div>

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

shadryck
shadryck
12,151 Points

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