473,231 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

If ... else that does not work

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
5 2029
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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JD | last post by:
I have the below code that displays a small thumbnail if a user is saluted, if they aren't saluted it checks to see if they might have a profile picture, this all works with the exception that is...
11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
6
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
9
by: TCMA | last post by:
I am looking for some tools to help me understand source code of a program written in C++ by someone else. Are there any non-commercial, open source C or C++ tools to reverse engineer C or C++...
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
20
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
4
by: Proaccesspro | last post by:
The code pasted below does not quite work as intended. I have a text box (txtSearch) on a form. Once the user enters a number (SSN) it willsearch for a match. One of the tests checks for a null or...
17
by: JRough | last post by:
I'm trying to get error proof code. I have this code which seems to work but now I look at it I think it should be elseif not else and I wonder why it works. It is in the block:...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.