kenwvs Posted August 18, 2006 Share Posted August 18, 2006 I am having trouble understanding how isset() works and what it means, and through a script I have been working on, it is used, but now I don't understand if it is working properly. I know that it means whether some variable is "set" or "unset", but I am not understanding what this means..... and how it works. Are there any tutorials that you know of tht explain this concept in basic terms, that a noobie should understand?Thanks,Ken Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 18, 2006 Share Posted August 18, 2006 isset() by example:[code]<?phpif(isset($_POST['submitbutton'])){//process form}?>//all your html<form action="page.php" method="post">//all your form fields<input type="submit" name="submitbutton" /></form>[/code]This is by far the most common use of the isset() function. The reason why it is used here is because the first time the page is loaded, the form would not have been submitted. Therefore, it is unnecessary to process the form and will likely lead to odd content shown to the user. So, by using isset() you only attempt to process the form if it has been submitted.Another common use is if you have a form with multiple submit buttons for differant actions. For instance, you might have a form on a forum such as this where you can post or preview, if you then imagine the following code:[code]<?phpif(isset($_POST['postreply']){//post the reply}if(isset($_POST['preview']){//preview the reply}?>[/code]you can see that by using isset, you can break up your code to only do the partiular actions relevant to the submit button that was pressed.Hope thats all clear Quote Link to comment Share on other sites More sharing options...
kenwvs Posted August 18, 2006 Author Share Posted August 18, 2006 if I read the first example[code]if(isset($_POST['submitbutton'])){[/code]does it bascally mean.... If the submit button is pressed, then submit the form....... or is it just checking to see if the submit button has been pressed. I am not clear how this works when the { } are at opposite ends of all the code. I have placed another posting a few minutes ago with an example of this question, where I am not clear.Thanks,Ken Quote Link to comment Share on other sites More sharing options...
hackerkts Posted August 18, 2006 Share Posted August 18, 2006 Yeah, it just check if the submit button had been submited. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 18, 2006 Share Posted August 18, 2006 No, what it means is:if the form has been submitted, then do everything between the curly braces, e.g. process the form, update the database etc.I dont quite understand what you mean by the curly braces being at opposite ends of the code, but if you mean this:if($var == 'foo'){//code}instead of:if($var == 'foor'){//code}Then they are identical. It is a matter of personal taste as to which people use. Quote Link to comment Share on other sites More sharing options...
kenwvs Posted August 18, 2006 Author Share Posted August 18, 2006 It is clearer now.... I meant if($var=='foo'){//code}versusif($var=='foo'){//only a couple lines of code and then the other curly.}but your explanation has cleared it up.....Thank You very much, I have been struggling over this and couldn't find a tutorial that really explains this..Ken Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 18, 2006 Share Posted August 18, 2006 Perhaps an example of not using isset might help...[code]<?php$var = $_POST['var'];if(empty($var){echo 'You did not provide any data';}else{echo "You variable was $var";}?><form action="page.php method="post><input type="text name="var" /><br /><input type="submit" name="submitbutton" /></form>[/code]If you try that, then the first time you visit the page, [b]before[/b] you have submitted the form, you will get 'You did not provide any data', which is obviously undesirable because it may confuse the user because they havn't tried to provide the data as yet.Also, if you consider the if statement to be:[code]<?phpif(empty($var){echo 'You did not provide any data';exit;}?>[/code]Then the script would also exit, meaning the user would never ever see the form. So, by putting isset() back in, we fix all of this:[code]<?phpif(isset($_POST['submitbutton']){$var = $_POST['var'];if(empty($var){echo 'You did not provide any data';}else{echo "You variable was $var";}}?><form action="page.php method="post><input type="text name="var" /><br /><input type="submit" name="submitbutton" /></form>[/code]Hope that helps :) Quote Link to comment Share on other sites More sharing options...
kenwvs Posted August 18, 2006 Author Share Posted August 18, 2006 This is awesome, it is finally making some sense to me...... you explain it very well!!! Have you considered writing a tutorial on basics like this?In the first example, with no isset() the page would display and exit as soon as you load it, not giving you the opportunity to input any data, hence the isset processes the data once you press the submit button..... this is awesome, as I can finally start to understand it..... I am excited, as stupid as that may sound!! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 18, 2006 Share Posted August 18, 2006 No problem. And one further note, from looking at your other post. you can also use isset() in the following way:[code]<?php//all your php code hereif(!isset($_POST['submitbutton']))//note the !{?>show form<?php}?>[/code]By doing this, you would only show the form if it has not been submitted. Sometimes this is desirable, othertimes it is not. So the use of it would depend on the situation. Quote Link to comment Share on other sites More sharing options...
kenwvs Posted August 18, 2006 Author Share Posted August 18, 2006 and that is how my other form is set up, so that would mean that it is automatically being submitted, as soon as you load the form.......... Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 18, 2006 Share Posted August 18, 2006 No, your other form is set up fine. Because when you first load the page, the form has not been submitted. Therefore the contents inside the curly braces after this lineif(!isset($_POST['submitbutton']))Are still executed, because the form has not been submitted. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.