Jump to content

Archived

This topic is now archived and is closed to further replies.

kenwvs

Looking for a Tutorial on isset()

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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 :)

Share this post


Link to post
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!!

Share this post


Link to post
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.

Share this post


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

Share this post


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

if(!isset($_POST['submitbutton']))

Are still executed, because the form has not been submitted.

Share this post


Link to post
Share on other sites

×

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.