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 trialJacob Prichard
2,174 PointsCan we use $_POST instead of $_GET to conditionally display thanks?
Is it possible to have a hidden field in the form, something like 'input type="hidden" name="thanks"', load that into $thanks = $_POST['thanks']; like we did with the other variables sent, and use that in the conditional instead, so we don't see the GET request info in the URL? I did try it out but couldn't get it to work.
2 Answers
Jason Cullins
Courses Plus Student 4,893 PointsYou absolutely can do exactly what you're talking about if you already have a form where the user is clicking a submit button to submit back to your page.
Try this
<?php
$thanks = $_POST['thanks'];
$name = $_POST['myname'];
if ( $thanks == '1' ) {
echo("Thank you {$name} for submitting this form!");
exit;
}
?>
<html>
<head>
<title>Submit Form Thanks Test</title>
</head>
<body>
<form method="POST" action="">
<input type="hidden" name="thanks" value="1">
<input type="text" name="myname" placeholder="Enter your name" value="">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
The {} are optional inside double quotes in this case, but you can use them to help your code read a bit better.
Basically the form will have 1 input box that asks the user for their name, when they click the submit button, because there was no action in the form it will just post to itself so then then when it sees that $thanks is equal to 1 then it will just send the echo statement and then simply exit.
Hope this helps!
Jacob Prichard
2,174 PointsThanks Jason!
Michell Ayala
7,433 PointsYeah, of course you can. But that technique is known as an anti-pattern. You should avoid that practice. When you are working with a database (mysql, sql, whatever) + PHP you should only use $_POST to send data to the server. For example; if you are creating a login form you need to use $_POST. Instead, if you are retriving data from the server, for example; http://teamtreehouse.com/user?username=some_user, you should us $_GET.
But that does not sound like a full explanation, you know. You can do anything that you want, even if that is not a standar way. (done).
Marcus Parsons
15,719 PointsMichell,
You probably want to edit your answer in the 2nd paragraph because you mixed up the way $_POST and $_GET work. $_GET sends information in the header, not $_POST. $_POST does not expose data unlike $_GET.
Jacob Prichard
2,174 PointsJacob Prichard
2,174 PointsThanks for the detailed description Michell! I want to mark your answer as the correct one, but it was posted as a comment, not an answer... but thanks again!