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

Prakhar Patwa
Prakhar Patwa
11,260 Points

Undefined variable

please have a look at my code, i have used bootstrap. My form is slightly different as well as the process. On the top of the page i have declared $results variable. But when i am trying to display it, It's showing me undefined variable

https://jsbin.com/kugebumino/edit?html,output

I saw your variable at the top of the page, but I couldn't find where you were trying to display it.

Prakhar Patwa
Prakhar Patwa
11,260 Points

@Ron baasland just before where the form tag , Sorry its a big chunk of html

It seems as though you haven't filled out your <form> declaration. It is not set to method="post" so the $_POST["submit"] is not available so the variable $results is not being set.

Prakhar Patwa
Prakhar Patwa
11,260 Points

@Ron i am not getting it, what are you trying to say?

Nevermind about my last comment, I was looking at something else.

Andrew McCormick
Andrew McCormick
17,730 Points

just a mod note that I cleaned up the question to be a little clearer

3 Answers

Since your submit button doesn't have a value declared $_POST["submit"] does not have a value so if($_POST["submit"]) is false so the $results variable never gets set. You can either add a value to your submit button, or set if($_POST["submit"] == "")

Andrew McCormick
Andrew McCormick
17,730 Points

Just adding to what Ron said, if you are going to echo the $results variable without checking to see if it's set first, then you need make sure it's set. Therefore when you declare it up top, rather than just using

<?php 
    if($_POST["submit"]){
        $results = "form has submitted"; 
    }
?>

use

<?php 
    if($_POST["submit"]){
        $results = "form has submitted"; 
    }else {
              $results = "no current submissions ";  //or something like that
       }
?>
Prakhar Patwa
Prakhar Patwa
11,260 Points

yess after breaking my head.....i got that. thanks to both of you.

Prakhar Patwa
Prakhar Patwa
11,260 Points

I have 1 more question

i haven't bought domain name yet, so if i put the email which i have created on ecowebhosting.com in the mail boxes option, That is why it is not sending?

But i recieve message when i put my gmail id in the code