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 trialKonrad Pilch
2,435 PointsPHP WHen should i use isset and when to ?
HI,
When should i use isset and when not?
I have a code like this
if(isset($_POST['submit'])){
That take the button , the submit button and check if its pressed by $_POST right? but , sometimes, when i use isset, it doesnt work . I just need only $_POST but in other cases i need this full lien of code.
And why theres a need to use isset and $_POST togheter? why does isset stands there for? and $_POST gets the value when clicked right ?
2 Answers
shiv sandhu
Full Stack JavaScript Techdegree Student 9,979 Points$_POST is an an array which holds information submitted through HTML forms via POST method. This array contain keys and values where keys are name of form controls and value are the input data from the users.
Form Example -
<html>
<form action="process.php" method="post">
<input type="text" name="firstname">
<input type="text" name="email" >
<input type="submit" name="submit" value="login">
</form>
</html>
In the above form the method was post here "name" attributes holds the form controls and whenever user click on login button ( as the value of submit input is login ) the forms input send the page called process.php.
Here is process.php -
<?php
if (isset($_POST['submit'])) {
echo "Your firstname is " . $_POST['firstname'] . "<br>";
echo "You email is " . $_POST['email'] ;
} else {
echo "Your form is not submitted yet please fill the form and visit again";
}
?>
In the above code in process.php the $_POST is a array which contains user information submitted by the form. Form send user input values with its control names to process.php. where firstname is key and whatever user type in input field will be the value in $_POST array and we can access that information like this $_POST['firstname'].
Now come to isset. isset function check that the variable is set or not in other words the variable has a value or not. In this case isset($_POST['submit']) checks that the $_POST['submit'] is set or not. it will be set if the user click on the login button. if the $_POST['submit'] is set then the code will echo users email and username else it will show that visit again message. For the purpose we you isset is that suppose if user visit process.php without submitting the form then the page will show an error that your $_POST['firstname'] and $_POST['email'] is not set as we are trying to echo out both of them. But now we are checking the form submission and if the user visit the process.php page without submitting the form. This time user will get the visit again message instead of error.
Hope this will help you to understand isset function and $_POST array.
Jorge Felico
15,543 PointsDid you set a value on the button? The value is what you would actually track in PHP to see if the button was pressed or not. Here's an example.
<input type="submit" value="Submit" >
if ( isset($_POST['Submit']) ){
//Code Here
}
Please note that in the $_POST argument, you need to pass it the value of the button which is case-sensitive.
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsThank you all and shiv sandhu , i reallu understand everything you write and its really clear . Your descriptions should be attached so google or even teachers notes so people can look for it : p. I do understand more from you than any other human : p
Thank you both very much and sepecially shivi sandhu.