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

Getting a blank page after I click on the submit button.

I'm following the code for my own website, but when I click on the submit form I get directed to a blank page. If I can get any insight as to why this is happening that would be amazing!

  <?php 
   if ($_SERVER['REQUEST_METHOD'] == "POST") {
$firstName = $_POST['inputFirstName'];
$lastName = $_POST['inputLastName'];
$email = $_POST['inputEmail'];
$message = $_POST['inputMessage'];

$email_body = "";
$email_body = $email_body . "You have a message from ";
$email_body = $email_body . $firstName." ";
$email_body = $email_body . $lastName;
$email_body = $email_body . " saying ";
$email_body = $email_body . "'".$message."'";
$email_body = $email_body . " use ".$email." as the email to take this to the next level!";

// TODO: Send email

header("Location: index.php?status=thanks#contact");
exit;
}

?>

.... 


    <div id="contact" class="container contactDiv">

        <h1 id="actionTitle">Contact</h1>

<!-- READ MORE ABOUT ISSET -->

        <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
                <p>Thanks for the email! Give me at lest 24 hours to come up with an amazing and personalized respons, directly from the heart.</p>

        <?php } else { ?>
                <p> Let's conect and create somthing beautiful!</p>

                <form method="post" action="">
                    <div class="form-inline">
                        <label for="inputFirstName">First Name</label>
                        <input type="text" name="inputFirstName" class="form-control" id="inputFirstName" placeholder="First Name">

                        <label for="inputLastName">Last Name</label>
                        <input type="text" name="inputLastName" class="form-control" id="inputLastName" placeholder="Last Name">

                        <label for="inputEmail">email</label>
                        <input type="email" name="inputEmail" class="form-control" id="inputEmail" placeholder="Email">
                      </div>

                      <div class="form-group">
                        <label for="inputMessage">Message</label>
                        <textarea type="text" name="inputMessage" class="form-control" id="inputMessage" placeholder="Talk to me, I'm a greate listener"></textarea>
                      </div>

                      <button type="submit" class="btn btn-default">Submit</button>
                </form>
            <?php } ?>

<!-- end .container -->
</div> </div>

</body> </html>

Andrew Sheragy
Andrew Sheragy
16,379 Points

This is working for me if I try it, assuming the code above all goes in a file index.php, if its another filename then it depends what your index.php looks like as the problem would be there. Is the redirect working? If it is then your web browser url should end with index.php?status=thanks#contact.

3 Answers

Hi Valerie,

Firstly let me give you this link here, it is the basics but has been extremely helpful to me.

Secondly I have spotted something that looks a little different to how I code if else statements in the way you have above.

I you a ":" not a "}" to chop it up across multiple lines like below:

                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

                    <div class="page-header">
                        <h1><?php the_title(); ?></h1>
                    </div>

                    <?php the_content(); ?>

                <?php endwhile; else: ?>

                    <div class="page-header">
                        <h1>Oh no!</h1>
                    </div>

                    <p>No content seems to be registered to this page please check your Pages and Template Settings!</p>

                <?php endif; ?>

I am not 100% if this is specific to WordPress but I can not see why it would be.

Hope this may help,

Craig

Hunter Dishner
Hunter Dishner
15,086 Points

I think you just need a value here

<form method="post" action="VALUE HERE YOURS IS BLANK">

Hunter Dishner
Hunter Dishner
15,086 Points

I think you just need a value here

<form method="post" action="VALUE HERE YOURS IS BLANK">