Jump to content

Looking for a Tutorial on isset()


kenwvs

Recommended Posts

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
Link to comment
Share on other sites

isset() by example:

[code]
<?php
if(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]
<?php
if(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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

It is clearer now.... I meant

if($var=='foo')
{
//code
}

versus

if($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
Link to comment
Share on other sites

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]
<?php
if(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]
<?php
if(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 :)
Link to comment
Share on other sites

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!!
Link to comment
Share on other sites

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 here
if(!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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.