Forum Moderators: coopster

Message Too Old, No Replies

IF Else Statement

Confusing error message

         

lubeguy

5:45 pm on Nov 3, 2005 (gmt 0)

10+ Year Member



I have a form being submitted from another page and being directed to a second page. In this form is a checkbox called nonprofit. When checked the value is 1.

On the second page I have the following code:

if($_POST['nonprofit']=='1'){
$nonprofit='1';
}else{
$nonprofit='0';
}

And in the body I have the folowing:

THE VALUE FOR nonprofit is <? echo $nonprofit;?>

When nonprofit is checked I get:
THE VALUE FOR nonprofit is 1

Which is what I expected. However when nonprofit is not checked I get the following:

Notice: Undefined Index: nonprofit ...etc.
THE VALUE FOR nonprofit is 0

The line that the notice refers to is:
if($_POST['nonprofit']=='1'){

I am a little confused by this error , since the ouput is what I wanted, so the if statement is working. Can anyone help me out with this.

jatar_k

5:51 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when a checkbox is checked it sends it's value, when it is not, nothing is sent

therefore

if($_POST['nonprofit']=='1'){

trips the undefined index notice

but this

if(!isset($_POST['nonprofit']) ¦¦ $_POST['nonprofit']=='1'){

would handle both cases

note: WebmasterWorld breaks pipes the ¦ must be replaced with a real pipe character

lubeguy

8:00 pm on Nov 3, 2005 (gmt 0)

10+ Year Member



I had to change it a little becuase your solution gave me the opposite results, but you helped me figure it out. I used:

if(!isset($_POST['nonprofit'])){
$nonprofit='0';
}else{
$nonprofit='1';
}

Thanks for the help. As always, this site is a help. I don't think there has been a single post I have put on here that has gone unsolved.

jatar_k

10:51 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



oops, yep I did that backwards, often do that when writing if's with && or ¦¦

glad you fixed it in spite of my foolishness

this would be what I meant

if(isset($_POST['nonprofit']) && $_POST['nonprofit']=='1'){