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 Checking the Request Method

location() with a status="thanks"

Hello all,

header('Location:contact.php?status="thanks"');

I was told that "thanks" is a variable but I am still unclear

1)Why is "thanks" a variable?

2)If thanks is a variable then, what is "status"

3) The original file name is contact-thanks.php, so does header('Location:contact.php?status="thanks"'); say to look for "thanks" inside a string that has says contact.php? I don't know how to phrase this question, but can you please explain in baby terms or 101 for dummies the connection between the original file called contact-thanks.php and header('Location:contact.php?status="thanks"');

Thanks!

Important Notice to all:

I believe some changes were made to the Post page that are affecting the the Best Answer option display.

Please make sure to reply to the above post under Add an Answer (it is way at the bottom of this entire page, so please scroll far down to post so that I can click on the best answer option ) section below*

Cheers all!

9 Answers

What you're trying to do here is preserve/send information to a page. In this case you're trying to send a state to the contact page.

When someone visits www.your-website.com/contact.php, they're requesting to see the file contact.php.

The server will answer the request by sending back the page contact.php, and this is the contact form you see on your screen.

A $_GET variable is a way of sending additional information along with your request to the server in the url.

You request the page contact.php but this time your passing through additional information. You passed through a key of status by visiting www.your-website.com/contact.php?status=thanks.

You can pass through anything via a $_GET param - there's no limit (well there is, but don't worry about that right now). You could say www.your-website.com/testfile.php?id=1&name=tome&lastname=cawthorn.

You access this information using the $_GET super global variable in the script of the destination page.E.g. in this example, on the testfile.php page, you could access that information in the url by:

<?php

$_GET['id'] // = 1
$_GET['name'] // = tom
$_GET['lastname'] // = cawthorn

Passing information in this way is not specific to php, it's "a thing on the internet".

1)Why is "thanks" a variable?

Because the status of the contact.php page is showing a thank you message. This is a custom name, so you might have ?status=error, or ?show=thankspage. It is totally open! The key name of status is most valid because you're talking about the status of the page. What you want to show is the thanks page, hence ?status=thanks. You could also have ?elephants=pink.

2)If thanks is a variable then, what is "status"

A custom key name

3) The original file name is contact-thanks.php, so does header('Location:contact.php?status="thanks"'); say to look for "thanks" .......

In this case, contact.php?status="thanks and contact.php will always go to the same contact.php file.

You are simply sending additional information to contact.php via the url.

Hope this helps

Ps. There are two reasons why you wouldn't be able to select a 'best answer'. Firstly if you've posted your question under 'general' and secondly if the answer was in the comments sections of the post as opposed to the answer section. In the latter, there's nothing you can do apart from ask the poster of the answer to move it downwards.

Oh, I just noticed your Ps, and super thanks to you. I suppose I should post all my questions under 'general'. But where is general?

***hmm... I have never had these problems before. Something drastic must have really changed on this site for the votes on posts not to appear all the time.

Steven Walters
Steven Walters
10,573 Points

Orange Sky,

Sorry my answer was erased here is quick recap.

Adding ?status=thanks sends a $_GET variable to the server along with the page request.

So $_GET["status"] == "thanks"

How this is used is explained in the next video.

status is the variable and thanks is the value you are passing.

--StevenKW

Hello Steven,

I had to repost because the previous one did not show Best Answer option and I didn't want to have you guys respond and not be able to vote for Best Answer

Also, I wasn't clear on any of the answers given, so I decided to repost with more specific questions.

I truly apologize for the inconvenience

Cheers!

Steven Walters
Steven Walters
10,573 Points

no problem... Does it make sense? I think it will after you see the next video.

Hello Steven,

I have watched the video a few times, but...

you said status is the variable name and thanks is the value name.

1) Let say I am on index.php, and I click on the contact button in the navigation bar. Can you please tell me how it affects status=thanks from clicking on that button on index.php

2) you said it sent a file to the server, what file is being sent.

3) I see in the next video that contact-thanks.php gets erased, so who does header("location:contact.php?status=thanks") send this varible and value to?

OH .... I am so confused.

Cheers!

Steven Walters
Steven Walters
10,573 Points

?status=thanks piggy backs with the page request and is sent to the php server to process.

when the server sees ?status=thanks the server stores the variable $_GET["status"] to the value "thanks"

The next video http://teamtreehouse.com/library/build-a-simple-php-application/adding-a-contact-form/working-with-get-variables explains how the $_GET variable is used.

Hello Steven,

What page request?

?status=thanks piggy backs with the page request and is sent to the php server to process.

Sorry, but I am failing to see the whole picture, but thanks for your help

Steven Walters
Steven Walters
10,573 Points

when you submit the form via POST an email is sent and then the code hits your

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

This makes a page request to the server for page contact.php and passes "thanks" to $_GET["status"]

Watch the Working with Get Variables video to see how this variable is used in a conditional to process your site.

You will get it. Just keep going forward a few steps and see how it all works.

Hello Tom,

This is so well explained. Let me just clarify some areas'

1)Why is "thanks" a variable?

Because the status of the contact.php page is showing a thank you message. This is a custom name, so you might have ?status=error, or ?show=thankspage. It is totally open! The key name of status is most valid because you're talking about the status of the page. What you want to show is the thanks page, hence ?status=thanks. You could also have ?elephants=pink.

Hello,

a)I am used to hearing variable and values。In terms of ?status=thanks, would it be safe to say that status is the variable which is storing the value thanks?

b) "Because the status of the contact.php page is showing a thank you message. " does this mean that we have purposely called the variable 'thanks' to describe the page it is referring to.

2)If thanks is a variable then, what is "status"

A custom key name

Hello,

c) In terms of "variable = value", is "key name" a substitute for 'value'

Thanks

In context of:

<?php
    header("Location: contact.php?status=thanks");
?>

1) Why is "thanks" a variable?

It is not. "thanks" is the value of URL query parameter "status", which gets passed on to $_GET["status"] global variable. This variable gets created on the fly, based upon the name and value of the URL query parameter.

2) If thanks is a variable then, what is "status"

Again, "thanks" is the value of URL query parameter "status", which gets passed on to $_GET["status"] global variable. This variable gets created on the fly, based upon the name and value of the URL query parameter.

3) The original file name is contact-thanks.php, so does header('Location:contact.php?status="thanks"'); say to look for "thanks" inside a string that has says contact.php? I don't know how to phrase this question, but can you please explain in baby terms or 101 for dummies the connection between the original file called contact-thanks.php and header('Location:contact.php?status="thanks"');

When we use "contact-thanks.php" file or "contact-process.php" file, we try to distribute our code to multiple files. But with the help of "superglobal" variables like $_GET or $_POST or $_SERVER and conditionals (if-else), we can integrate all our code into one file. We do this for optimisation, as this not only saves space, but also helps in maintenance.

So with the help of $_SERVER and $_GET variables, we merged the code of 3 files (contact.php, contact-process.php and contact-thanks.php) into 1 file (contact.php):

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Run contact-process.php code
    header("Location: contact.php?status=thanks");
    exit;
}
...
if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {
    // Run contact-thanks.php code
} else {
    // Run contact.php code
}
?>

$_GET is an "associative array", where "status" is a unique key (variable) that holds up the value of "thanks".

Hope this helps!