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

$_GET is not working

When I click the submit button on the form it isnt redirecting me to header(Location: register.php?success); does anyone else see what this isnt working.

<?php 
include 'core/init.php';
logged_in_redirect();
include 'includes/overall/header.php';

if (empty($_POST) === false) {
    $required_fields = array('first_name', 'last_name', 'username', 'password', 'password_again', 'email');
    foreach($_POST as $key=>$value) {
        if (empty($value) && in_array($key, $required_fields) === true) {
            $errors[] = 'All field are required';
            break 1;
        }
    }

    if (empty($errors) === true) {
        if (user_exists($_POST['username']) === true) {
            $errors[] = 'Sorry, the user \'' . $_POST['username'] . '\' is already taken';
        }
        if (preg_match("/\\s/", $_POST['username']) == true) {
            $errors[] = 'Your username can not have spaces';

        }
        if (strlen($_POST['password']) < 6) {
            $errors[] = 'Your password must be at least 6 characters long';
        }

        if ($_POST['password'] !== $_POST['password_again']) {
            $errors[] = 'Your passwords do not match';
        }

        if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
            $errors[] = 'A valid email address is required';
        }

        if (email_exists($_POST['email']) === true) {
            $errors[] = 'Sorry, the email \'' . $_POST['email'] . '\' is already is use. Please contact your site adimn is this is incorrect.';
        }

    }
}
 ?>

    <div class="container background">
        <?php
        if (isset($_GET['success']) && empty($_GET['success'])) {
            echo 'You\'ve be registered successfully! Please check your email to activate your account';
        } else {
            if(empty($_POST) === false && empty($errors) === true) {
                $register_data = array(
                    'first_name'    => $_POST['first_name'],
                    'last_name'     => $_POST['last_name'],
                    'username'  => $_POST['username'],
                    'password'      => $_POST['password'],
                    'email'         => $_POST['email'],
                    'email_code'    => md5($_POST['username'] + microtime())
                );

                register_user($register_data);
                header('Location: register.php?success');
                exit();

            } else if (empty($errors) === false) {
                echo output_errors($errors);
            }

        ?>
            <form action="" method="POST" class="form-horizontal" role="form">
              <div class="form-group">
                <label for="inputfirstname3" class="col-sm-2 control-label">First Name</label>
                <div class="col-sm-10">
                  <input type="text" name="first_name" class="form-control" id="inputfirstname3" value="" autocomplete="off" placeholder="First Name">
                </div>
              </div>
              <div class="form-group">
                <label for="inputlastname3" class="col-sm-2 control-label">Last Name</label>
                <div class="col-sm-10">
                  <input type="text" name="last_name" class="form-control" id="inputlastname3" value="" autocomplete="off" placeholder="Last Name">
                </div>
              </div>
              <div class="form-group">
                <label for="inputusername3" class="col-sm-2 control-label">Username</label>
                <div class="col-sm-10">
                  <input type="text" name="username" class="form-control" id="inputusername3" value="" autocomplete="off" placeholder="Username">
                </div>
              </div>
              <div class="form-group">
                <label for="inputemail3" class="col-sm-2 control-label">Email</label>
                <div class="col-sm-10">
                  <input type="email" name="email" class="form-control" id="inputemail3" value="" autocomplete="off" placeholder="Email Address">
                </div>
              </div>
              <div class="form-group">
                <label for="inputpassword3" class="col-sm-2 control-label">Password</label>
                <div class="col-sm-10">
                  <input type="password" name="password" class="form-control" id="inputpassword3"  autocomplete="off" placeholder="Password">
                </div>
              </div>
              <div class="form-group">
                <label for="inputpassword_again3" class="col-sm-2 control-label">Validate Password</label>
                <div class="col-sm-10">
                  <input type="password" name="password_again" class="form-control" id="inputpassword_again3" autocomplete="off" placeholder="Validate Password">
                </div>
              </div>
              <div class="form-group">
                <div class="col-sm-10">
                  <p class="info">Please be aware that this does not mean that you are a member of Better Gamerz United. If you would like to be come a member please
                  contuine with the registration and then fill out our application. Other wise you will have limited use of this site. Thank you Team BGU!! </p>
                </div>
              </div>
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-primary pull-right register">Register</button>
                </div>
              </div>
            </form>
        <?php } ?>
    </div>
<?php include 'includes/overall/footer.php'; ?>

2 Answers

Hi Andrew,

You can't have any output to the page before a header redirect or it won't work.

You can read more about that here: http://php.net/manual/en/function.header.php

You have included your page header near the top and presumably that's outputting all the html for the header section and also between your 1st and 2nd php block you're outputting that container div.

So all of that would cause header not to work. It needs to be taken care of nearer the top before you output anything.

I would recommend that you go through the "Build a simple php application" course if you haven't already done so because it covers how to do a contact form. Or review the project code if you have already completed it.

I did what you said and it works. I have another issue on another page if you wouldn't mind helping

Ok, make sure you test it. I didn't really look at the code in detail so I don't know if everything will be working properly if you just move that bit of code to the top.

It may help to take a look at the "contact.php" file from the php course to see how things are done there.

I can try to help but I would recommend you make a new post. You may not get too many people looking at this one anymore since you've selected "best answer". Plus it's probably a good idea to keep each discussion to one problem.

You can post the link here if you decide to start a new one.

That header is only ran when the form is submitted. Im not understanding what your saying