Jump to content
Larry Ullman's Book Forums

Recommended Posts

I posted this on the web site "errata" page for this book. I keep getting an "undefined index" error when I don't click the "terms" checkbox on the form. But it seemed to work fine when I changed the following:

 

//Validate the terms:

if ( !isset($_POST['terms']) AND ($_POST['terms']
==
'Yes') ) {

echo '<p class="error">You must accept the terms.</p>';

$okay = FALSE;

}

 

to

 

//Validate the terms:

if ( !isset($_POST['terms']) AND ($_POST['terms']
=
'Yes') ) {

echo '<p class="error">You must accept the terms.</p>';

$okay = FALSE;

}

 

So I figured the double equal signs were a typo since it SEEMED to work as it should, but when I posted the "mistake", Larry said that it isn't a typo that he think I must have something wrong with registration form. I looked and looked, and looked.... tried different things (comparing mine to Larry's, taking out tables I added for aesthetics, etc...) and nothing helped fix the problem. So I decided today to try Larry's scripts (the one's he provided for download) instead of mine, and the same error is occurring.

 

What could be the problem?

Link to comment
Share on other sites

Actually, my apologies, there is an error in the text, it's just not the error you think it is. First of all, the code should technically be just

if (!isset($_POST['terms'])) {

The second condition was suggested on page 136, but not formally added to the script. So it shouldn't appear in subsequent scripts.

 

But as for the error, the first clause is true if $_POST['terms'] isn't set. The second part of the clause needs to check the value $_POST['terms'] should have. Since we're trying to identify problems, the second clause should actually be

OR ($_POST['terms'] != 'Yes')

because the corresponding actions should be taken if $_POST['terms'] is not set or if it does not have the value of Yes. (If we were trying to find a positive match, the code would be isset($_POST['terms']) and ($_POST['terms'] == 'Yes').)

 

Apologies again for the confusion. I think I was confused by your switch to a single quotation mark, which, as I've said before, doesn't actually solve the problem and does, in fact, introduce a bug into the system.

 

Let me know if any of this is still not clear.

Link to comment
Share on other sites

Actually, my apologies, there is an error in the text, it's just not the error you think it is. First of all, the code should technically be just

if (!isset($_POST['terms'])) {

The second condition was suggested on page 136, but not formally added to the script. So it shouldn't appear in subsequent scripts.

 

But as for the error, the first clause is true if $_POST['terms'] isn't set. The second part of the clause needs to check the value $_POST['terms'] should have. Since we're trying to identify problems, the second clause should actually be

OR ($_POST['terms'] != 'Yes')

because the corresponding actions should be taken if $_POST['terms'] is not set or if it does not have the value of Yes. (If we were trying to find a positive match, the code would be isset($_POST['terms']) and ($_POST['terms'] == 'Yes').)

 

Apologies again for the confusion. I think I was confused by your switch to a single quotation mark, which, as I've said before, doesn't actually solve the problem and does, in fact, introduce a bug into the system.

 

Let me know if any of this is still not clear.

 

YAY! That fixed it! Thanks so much! It's been driving me absolutely insane! It makes so much sense now that it's explained. I actually feel like an idiot for not figuring that out on my own. lol I know... I could have just used the original method with no issues, and move on... but noooooo..... I "needed" to get THIS way working simply because it wasn't. lol

 

Many thanks!

Link to comment
Share on other sites

You're quite welcome. Apologies again for the confusion and for not catching this myself earlier. And, of course, I'm the idiot that made that mistake in the first place.

 

For what it's worth, I'd say it's a good thing that you feel the need to make sure you figure it out: it's the sign of a good programmer! In hindsight, do you understand now why the single equals sign makes the error go away but why it also introduces a bug?

Link to comment
Share on other sites

You're quite welcome. Apologies again for the confusion and for not catching this myself earlier. And, of course, I'm the idiot that made that mistake in the first place.

No problem! Just reimburse me for my involuntary visit to the local psychiatric center and we'll call it even. lol

 

For what it's worth, I'd say it's a good thing that you feel the need to make sure you figure it out: it's the sign of a good programmer!

Thanks!

 

In hindsight, do you understand now why the single equals sign makes the error go away but why it also introduces a bug?

Yes, I do actually. Well not "clearly" (lol) but for the most part I understand that "=" isn't "equal to" and that "==" does mean "equal to", and that using one or the other can changes things. But to really grasp exactly HOW it changes things is a little fuzzy in some areas. I think what's making it harder to grasp is when I changed it to one "=" it seemed to work as it should.

 

Crazy stuff. I'm sure if I really sit here and think it through it will become more clear to me though.

 

---------------

P.S. the psych ward visit was $350.00 :P

Link to comment
Share on other sites

A single = sign means the value is getting assigned to a value where as == is a comparison operator (i.e. testing that one thing is equal to another).

 

So with a single = sign the value of 'Yes' is being assigned to $_POST['terms'] which will return TRUE in a conditional check.

 

e.g.

 

<?php

if ( !isset($_POST['terms']) AND ($_POST['terms'] = 'Yes') ) {
 echo "BUG!!";
}
echo $_POST['terms'];

?>

 

That code will echo out BUG!! and Yes because it has been assigned to $_POST['terms']. If you changed it to:

 

<?php

if ( !isset($_POST['terms']) AND ($_POST['terms'] = FALSE) ) {
 echo "BUG!!";
}
echo $_POST['terms'];

?>

 

It would output nothing - correctly. Using a comparison operator would never output anything because if it's not set it can't also be equal to 'Yes'

 

Does that make it any clearer?

Link to comment
Share on other sites

Hi Stuart! Thanks for taking the time to explain things to me. Much appreciated.

 

The wording and example is a bit confusing for a newb like me, but let me give it a shot:

 

When we use one equal sign, we're saying that the variable IS that value... say "yes" in this example, which will make the variable always result in being TRUE regardless if the user checks yes or not on the form?

But when you use two equal signs, you're comparing the value of the variable to make sure that the value has a value of "yes", so that if a user chooses "no" for example, it will return a FALSE but if they choose "yes" it will return a TRUE?

 

Close?

Link to comment
Share on other sites

Yep you got it! :)

 

The only caveat is that because its a checkbox the value is only sent to the server if the checkbox is selected by the user - thats why in this instance you don't need to check it's value is equal to 'Yes' just that indeed the value isset in the $_POST array.

Link to comment
Share on other sites

Yep you got it! :)

 

The only caveat is that because its a checkbox the value is only sent to the server if the checkbox is selected by the user - thats why in this instance you don't need to check it's value is equal to 'Yes' just that indeed the value isset in the $_POST array.

 

Got it! Awesome! Thanks again! :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...