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
Jeff Busch
19,287 Points$_POST and $_GET
OK, so I'm a bit confused here. In one of the videos for Build a Simple PHP Application, Randy is checking the $_GET variable in an if statement. How is this variable being used/set when the form method attribute is set to post? If I do a little test with a small form and var_dump both $_POST and $_GET I get nothing in $_GET when method is set to post and if I set method to get var_dump returns nothing for $_POST. So how is $_GET being used in Build a Simple PHP Application website?
Jeff
2 Answers
Gareth Borcherds
9,372 PointsThe $_GET variable can also be used to get parameters passed in the URL. So because this is a shirt detail page, you can use one page for all shirts by passing the shirt ID through the URL. So if you put at the end of the URL ?id=1 the $_GET['id'] will contain the value from the URL header.
You just need to remember that get on a form simply passes the form data through the URL and that you can pass get data from any redirect, link, or whatever if you control the browser destination and can append variables after a ? At the end of the URL.
Jeff Busch
19,287 PointsHi Gareth,
Thank you for replying. Sorry I haven't gotten back sooner. I think I have a fairly good understanding of the differences between the two variables. If you have a line of code like this, contact.php?status=thanks, php will by default place the key and value into $_GET.
I've been playing with the Project Files from the simple php app videos. In the contact.php file I have placed var_dumps all over the file which I have supplied below. No matter where I place the var_dumps $_POST comes up empty. The only way I can get var_dump($_POST) to display anything is if I comment out header("Location: contact.php?status=thanks"); exit; on line 84. So I'm a bit confused as to what is actually happening here. Doe this line of code override the form method="post"? I'm guessing no because the form information never shows up in var_dump($_GET). I would like to understand what is going on here.
Thanks for your patience, Jeff
<!--***** My test code *****-->
<pre>
<hr>
<?php echo 'At top of file.<br>'; ?>
<?php echo 'var_dump($_POST)<br><br>'; ?>
<?php var_dump($_POST); ?>
<hr>
<?php echo 'var_dump($_GET)<br><br>' ?>
<?php var_dump($_GET); ?>
<hr>
</pre>
<!-- ********************** -->
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == "") {
echo "You must specify a value for name, email address, and message.";
exit;
}
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
echo "There was a problem with the information you entered.";
exit;
}
}
if ($_POST["address"] != "") {
echo "Your form submission has an error.";
exit;
}
require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)){
echo "You must specify a valid email address.";
exit;
}
$name = "";
$email = "";
$message = "";
$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom($email, $name);
$address = "orders@shirts4mike.com";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->Subject = "Shirts 4 Mike Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "There was a problem sending the email: " . $mail->ErrorInfo;
exit;
}
?>
<!--***** My test code *****-->
<pre>
<hr>
<?php echo 'Before header("Location:...)<br>'; ?>
<?php echo 'var_dump($_POST)<br><br>'; ?>
<?php var_dump($_POST); ?>
<hr>
<?php echo 'var_dump($_GET)<br><br>' ?>
<?php var_dump($_GET); ?>
<hr>
</pre>
<!-- ********************** -->
<?php
header("Location: contact.php?status=thanks");
exit;
}
?>
<?php
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>
<!--***** My test code *****-->
<pre>
<hr>
<?php echo 'After inc/header.php<br>'; ?>
<?php echo 'var_dump($_POST)<br><br>'; ?>
<?php var_dump($_POST); ?>
<hr>
<?php echo 'var_dump($_GET)<br><br>' ?>
<?php var_dump($_GET); ?>
<hr>
</pre>
<!-- ********************** -->
<div class="section page">
<div class="wrapper">
<h1>Contact</h1>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php } else { ?>
<p>I’d love to hear from you! Complete the form to send me an email.</p>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="text" name="email" id="email">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
<tr style="display: none;">
<th>
<label for="address">Address</label>
</th>
<td>
<input type="text" name="address" id="address">
<p>Humans (and frogs): please leave this field blank.</p>
</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php } ?>
</div>
</div>
<!--***** My test code *****-->
<pre>
<hr>
<?php echo 'After form Before footer.php<br>'; ?>
<?php echo 'var_dump($_POST)<br><br>'; ?>
<?php var_dump($_POST); ?>
<hr>
<?php echo 'var_dump($_GET)<br><br>' ?>
<?php var_dump($_GET); ?>
<hr>
</pre>
<!-- ********************** -->
<?php include('inc/footer.php') ?>