By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

If statements when submitting form

P: n/a
I'm setting up a form and want to check if some conditions are met when
the form submits. If they aren't, the form displays a missing field
message and the user has to go back to fill in the missing data. If it
is met, the form continues processing. I have the $state variable coming
from a drop down SELECT option with "" being the SELECTED default.

The if statements are included inside the form tags on the form page,
not on the processing page.

<?php
if (isset($state)) { //if state is set, require the following fields
?>
<input type="hidden" name="require"
value="name,address,city,state,zipcode,phone,email ,question">
<?php
}
if (!isset($state)) { //if state is not set, require the following fields
?>
<input type="hidden" name="require"
value="name,address,city,country,zipcode,phone,ema il,question">
<?php
}

These both work properly. But where I'm running into trouble is when I
want to further define the if statement to contain like:

if ((!isset($state)) && ($country == "USA")) {
?>
<input type="hidden" name="require"
value="name,address,city,state,zipcode,phone,email ,question">
<?php
}

This doesn't work. It will allow the empty $state and the value "USA" in
$country to submit. How can I prevent this?

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Jack wrote:
....
This doesn't work. It will allow the empty $state and the value "USA" in
$country to submit. How can I prevent this?


Put this inside the second if()

if (isset($state)) { /* whatever */ }
if (!isset($state)) {
if ($country == "USA") {
/* whatever */
} else {
/* whatever */
}
}
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
Jack wrote:
...
This doesn't work. It will allow the empty $state and the value
"USA" in $country to submit. How can I prevent this?


Put this inside the second if()

if (isset($state)) { /* whatever */ }
if (!isset($state)) {
if ($country == "USA") {
/* whatever */
} else {
/* whatever */
}
}


I agree the solution you give is simpler, but can't see _why_ the original
won't work. Can you point it out?
Always having a hard time with boolean logic...
Jul 17 '05 #3

P: n/a
Pjotr Wedersteers wrote:
Pedro Graca wrote:
Put this inside the second if()

if (isset($state)) { /* whatever */ }
if (!isset($state)) {
if ($country == "USA") {
/* whatever */
} else {
/* whatever */
}
}


I agree the solution you give is simpler, but can't see _why_ the original
won't work. Can you point it out?
Always having a hard time with boolean logic...


Original code simplified, reformatted, and numbered:
1 <?php
2 if (isset($state)) { echo 'a'; }
3 if (!isset($state)) { echo 'b'; }
4 if ((!isset($state)) && ($country == "USA")) { echo 'c'; }
5 ?>
When isset($state) is false this script will echo 'b' (line 3) no matter
what is the value of $country.

If, $country is 'USA' and $state is not set, the script will echo 'c'
(line 4), right after having echoed 'b';

The tests for
isset($state)
and
$country == 'USA'
are independent, but should be tested together.
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #4

P: n/a
Pedro Graca wrote:
Pjotr Wedersteers wrote:
Pedro Graca wrote:
Put this inside the second if()

if (isset($state)) { /* whatever */ }
if (!isset($state)) {
if ($country == "USA") {
/* whatever */
} else {
/* whatever */
}
}


I agree the solution you give is simpler, but can't see _why_ the
original won't work. Can you point it out?
Always having a hard time with boolean logic...


Original code simplified, reformatted, and numbered:
1 <?php
2 if (isset($state)) { echo 'a'; }
3 if (!isset($state)) { echo 'b'; }
4 if ((!isset($state)) && ($country == "USA")) { echo 'c'; }
5 ?>
When isset($state) is false this script will echo 'b' (line 3) no
matter what is the value of $country.

If, $country is 'USA' and $state is not set, the script will echo 'c'
(line 4), right after having echoed 'b';

The tests for
isset($state)
and
$country == 'USA'
are independent, but should be tested together.

Of course! It must have been a bad day, I missed this... Tsss. Thanks Pedro!
Jul 17 '05 #5

P: n/a
Pedro Graca wrote:
Pjotr Wedersteers wrote:
Pedro Graca wrote:
Put this inside the second if()

if (isset($state)) { /* whatever */ }
if (!isset($state)) {
if ($country == "USA") {
/* whatever */
} else {
/* whatever */
}
}


I agree the solution you give is simpler, but can't see _why_ the original
won't work. Can you point it out?
Always having a hard time with boolean logic...

Original code simplified, reformatted, and numbered:
1 <?php
2 if (isset($state)) { echo 'a'; }
3 if (!isset($state)) { echo 'b'; }
4 if ((!isset($state)) && ($country == "USA")) { echo 'c'; }
5 ?>
When isset($state) is false this script will echo 'b' (line 3) no matter
what is the value of $country.

If, $country is 'USA' and $state is not set, the script will echo 'c'
(line 4), right after having echoed 'b';

The tests for
isset($state)
and
$country == 'USA'
are independent, but should be tested together.


Thank you Pedro. While it's great to have someone write a working code
to correct my mistake, it's always nice to see an explanation of why
something I wrote did or didn't work. Seeing this was a big help in
understanding where I went wrong.

Jack

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.