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

If ... else that does not work

P: n/a
I'm sure I'm missing something blindingly obvious here. I am relatively new
to PHP, so excuse dumb mistakes.

I a trying to check a form submitted from an earlier page, and check $_POST
for empty values. Only some of them are not allowed to be empty, hence the
rather lengthy way in which the script is written. If no required fields are
empty, then I want another part of the script to function, and the variables
to be sent to a database. Easy eh? I thought I should be able to simply use
a big 'if ... else' statement. The PHP is at the bottom.

What happens in practice is that PHP is interpreting the first if statement
as always true, even when the $_POST variables are not == "", and the 'else'
part of the PHP never fires. Can anybody spot the idiot mistake I am making?

I have obviously omitted some PHP for ease of reading, but nothing which
should effect the program.

if ($_POST['subscription_rate'] or $_POST['subscription_type'] or
$_POST['first_name'] or $_POST['last_name'] or $_POST['address_street'] ==
"")
{
echo "<table width=\"500\" align=\"center\" bgcolor=\"white\"><tr><td>\n
<form target=\"checkblank.php\" method=\"post\">";
if ($_POST['subscription_rate'] == "")
{
echo "part of a form";
}
if ($_POST ['subscription_type'] == "")
{
echo "another part of a form";
}
if ($_POST ['first_name'] == "")
{
echo "another part of a form";
}
if ($_POST ['last_name'] == "")
{
echo "another part of a form";
}
if ($_POST ['address_street'] == "")
/*open 2ndary if clause*/
{
echo "another part of a form";
}
echo "<input type=\"reset\" value=\"Reset\">&nbsp;<input type=\"submit\"
value=\"Submit\">\n";
}

else
{
/*submit all the details to MySQL database*/
}
Many thanks for advice

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


P: n/a
I'm pretty new to PHP myself, and I'm not totally sure what your
problem is, but I'll take a stab at it.... my guess is that your
statement:

if ($_POST['subscription_rate'] or $_POST['subscription_type'] or
$_POST['first_name'] or $_POST['last_name'] or $_POST['address_street']
==
"")

should be

if ($_POST['subscription_rate'] == ""
or $_POST['subscription_type'] =="" or
$_POST['first_name'] =="" or .......

I'm thinking maybe it's only checking if the last one is == to "" and
evaluating the others as boolean statements?

This is probably totally wrong, so feel free to disregard all of it.
:)

PulsarSL

Jul 17 '05 #2

P: n/a
Joe Blow wrote:
I'm sure I'm missing something blindingly obvious here. I am
relatively new to PHP, so excuse dumb mistakes.

I a trying to check a form submitted from an earlier page, and check
$_POST for empty values. Only some of them are not allowed to be
empty, hence the rather lengthy way in which the script is written. If
no required fields are empty, then I want another part of the script
to function, and the variables to be sent to a database. Easy eh? I
thought I should be able to simply use a big 'if ... else' statement.
The PHP is at the bottom.

What happens in practice is that PHP is interpreting the first if
statement as always true, even when the $_POST variables are not ==
"", and the 'else' part of the PHP never fires. Can anybody spot the
idiot mistake I am making?

I have obviously omitted some PHP for ease of reading, but nothing
which should effect the program.

if ($_POST['subscription_rate'] or $_POST['subscription_type'] or
$_POST['first_name'] or $_POST['last_name'] or
$_POST['address_street'] == "")


What you are testing for here is

$_POST['subscription_rate'] contains a value
OR
$_POST['subscription_type'] contains a value
OR
$_POST['first_name'] contains a value
OR
$_POST['last_name'] contains a value
OR
$_POST['address_street'] is equal to an empty string

From what you have posted you appear to think that having the comparison
at the end evaluates the comparison for *all* of the variables.

What you should be doing is this:

if ($_POST['subscription_rate'] == "" or $_POST['subscription_type'] ==
"" or $_POST['first_name'] == "" or $_POST['last_name'] == "" or
$_POST['address_street'] == "")

[rest of post snipped]

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #3

P: n/a

"Chris Hope" <bl*******@electrictoolbox.com> wrote in message
news:d6**********@lust.ihug.co.nz...
Joe Blow wrote:
I'm sure I'm missing something blindingly obvious here. I am
relatively new to PHP, so excuse dumb mistakes.

I a trying to check a form submitted from an earlier page, and check
$_POST for empty values. Only some of them are not allowed to be
empty, hence the rather lengthy way in which the script is written. If
no required fields are empty, then I want another part of the script
to function, and the variables to be sent to a database. Easy eh? I
thought I should be able to simply use a big 'if ... else' statement.
The PHP is at the bottom.

What happens in practice is that PHP is interpreting the first if
statement as always true, even when the $_POST variables are not ==
"", and the 'else' part of the PHP never fires. Can anybody spot the
idiot mistake I am making?

I have obviously omitted some PHP for ease of reading, but nothing
which should effect the program.

if ($_POST['subscription_rate'] or $_POST['subscription_type'] or
$_POST['first_name'] or $_POST['last_name'] or
$_POST['address_street'] == "")


What you are testing for here is

$_POST['subscription_rate'] contains a value
OR
$_POST['subscription_type'] contains a value
OR
$_POST['first_name'] contains a value
OR
$_POST['last_name'] contains a value
OR
$_POST['address_street'] is equal to an empty string

From what you have posted you appear to think that having the comparison
at the end evaluates the comparison for *all* of the variables.

What you should be doing is this:

if ($_POST['subscription_rate'] == "" or $_POST['subscription_type'] ==
"" or $_POST['first_name'] == "" or $_POST['last_name'] == "" or
$_POST['address_street'] == "")

[rest of post snipped]

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com


Brilliant, many thanks to both of you. As I suspected, it is a bit obvious
once it is pointed out. I had it your way the other day and changed it for
some reason when struggling with something else.

JB
Jul 17 '05 #4

P: n/a
I noticed that Message-ID: <ne****************@read1.cgocable.net> from
Joe Blow contained the following:
Brilliant, many thanks to both of you. As I suspected, it is a bit obvious
once it is pointed out. I had it your way the other day and changed it for
some reason when struggling with something else.


When you are testing every element of an array (in this case the $_POST
array) you may find it more efficient to use a loop.

$missing="";
foreach($_POST as $key=>$value){
if($value==""){
$missing="<li>$key</li>";
}
}

if ($missing!=""){
echo "Oops, you missed out <ul>$missing</ul>"
}

Untested.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5

P: n/a
Geoff Berrow wrote:
I noticed that Message-ID: <ne****************@read1.cgocable.net>
from Joe Blow contained the following:
Brilliant, many thanks to both of you. As I suspected, it is a bit
obvious once it is pointed out. I had it your way the other day and
changed it for some reason when struggling with something else.


When you are testing every element of an array (in this case the
$_POST
array) you may find it more efficient to use a loop.

$missing="";
foreach($_POST as $key=>$value){
if($value==""){
$missing="<li>$key</li>";
}
}

if ($missing!=""){
echo "Oops, you missed out <ul>$missing</ul>"
}

Untested.


Or you could do it like this, if you only require some of the fields to
be completed:

foreach(array('foo', 'bar', 'baz') as $fieldname) {
if($_POST[$fieldname] == '') {
...
}
}

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.