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