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 Basic PHP Website (2018) Adding a Basic Form Checking the Request Method

James Barrett
James Barrett
13,253 Points

Alena's use of PHP tags - struggling to understand?

Hi! I am applying Alena's technique of validating PHP forms to my own website. I am struggling to understand how the PHP tags are working?

<?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
           echo "<h1>Your email has been sent successfully</h1>";
        } else { ?>
          <h1>Register, it's free!</h1>
          <form id="register" name="register" action="register.php" method="post">
              <div class="social-register">
                  <a href="#"><img class="twitter" src="img/twitter.png"></a>
                  <a href="#"><img class="facebook" src="img/facebook.png"></a>
              </div>
              <p class="or">Or</p>
              <label for="forename">Forename</label>
              <input type="text" id="forename" name="user_forename" placeholder="John" required>
              <span id="errorforename" class="error">First name is required</span>
              <label for="surname">Surname</label>
              <input type="text" id="surname" name="user_surname" placeholder="Smith" required>
              <span id="errorsurname" class="error">Surname is required</span>
              <label for="email">Email</label>
              <input type="text" id="email" name="user_email" placeholder="Example: johnsmith@mail.com" required>
              <span id="erroremail" class="error">Invalid Email</span>
              <label for="password">Password</label>
              <input type="password" id="password" name="user_password" placeholder="&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;" required>
              <span id="errorpassword" class="error">Password must be longer than 6 characters</span>
              <label for="team">Favourite team</label>
              <select id="team" name="user_team" required>
                  <option value="pleaseselect">Please Select</option>
                  <option value="arsenal">Arsenal</option>
                  <option value="astonvilla">Aston Villa</option>
                  <option value="bournemouth">Bournemouth</option>
                  <option value="chelsea">Chelsea</option>
                  <option value="crystalpalace">Crystal Palace</option>
                  <option value="everton">Everton</option>
                  <option value="leicester">Leicester</option>
                  <option value="liverpool">Liverpool</option>
                  <option value="mancity">Man City</option>
                  <option value="manunited">Man United</option>
                  <option value="newcastle">Newcastle</option>
                  <option value="norwich">Norwich</option>
                  <option value="southampton">Southampton</option>
                  <option value="stoke">Stoke</option>
                  <option value="sunderland">Sunderland</option>
                  <option value="swansea">Swansea</option>
                  <option value="tottenham">Tottenham</option>
                  <option value="watford">Watford</option>
                  <option value="westbrom">West Brom</option>
                  <option value="westham">West Ham</option>
              </select>
              <span id="errorteam" class="error">Please select a team</span>
              <label for="bio">Biography</label>
              <textarea id="bio" name="user_bio" placeholder="Hi, I am a member of Footdrive!" required></textarea>
              <span id="errorbio" class="error">Please enter something!</span>
              <label for="humancheck">Are you human? What is 2+2</label>
              <input type="text" id="human" name="user_human" placeholder="What is 2+2?" required>
              <span id="errorhuman" class="error">Answer is incorrect</span>
              <button type="submit" value="Submit" onclick="return validate();">Register</button>
      </div>
      </form>
        <?php } ?>

Why are we closing our PHP tag after initiating the 'else' statement? Then we open a new set of php tags before the end of the form and close it after the curly brace? Struggling to get my head around it. Any help would be brilliant!

Thanks, James.

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey James,

It does get a bit confusing, and I'll try to make sense.

Basically, you are using 2 separate languages in one sheet. PHP which is a back-end language and gets processed by the server... and HTML which is front-end and get processed by the browser.

In the beginning (the if else statement) PHP is being used to test whether or not form data has been set and sent. If it has, the server will process this and then return (echo "<h1>Your email has been sent successfully</h1>";) back into the HTML. The echo statement basically (in a sense) prints to the browser directly. That line is surrounded by HTML tags, so the browser interprets and processes it as such, thus you get the output as an level one heading in the browser window.

Now, if the form data wasn't set or sent, we want something else to happen (Show the form). The PHP tag is closed out after the opening curly brace of the else statement because this tells the Server processing the PHP that unless the data is set / sent... then we want something else done. That something else in this case is putting up the form for the user to fill out. This is done on the client-side (front-end) with HTML. We don't want the server to be serving up the form, we want our hard-coded HTML to do that, so we need to close out the PHP code... so the browser can process the HTML. Remember, the browser cannot process PHP, and unlike the h1 tag that PHP returned, we don't want the server to process and serve up the entire form code.

When the form is finished being built, we do still need to close the else statement, but because the closing curly brace is for the PHP code, it needs to be wrapped in PHP tags.

In short, we have the PHP conditional that if it moves to the else, PHP takes a 'time-out' and allows the HTML to compile, and then when the HTML is done, the PHP gets closed out.

I hope this makes sense and helps to clear it up some. Keep Coding! :)

James Barrett
James Barrett
13,253 Points

Excellent, thanks for your answer :)