473,789 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_r ate'] or $_POST['subscription_t ype'] 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=\"checkb lank.php\" method=\"post\" >";
if ($_POST['subscription_r ate'] == "")
{
echo "part of a form";
}
if ($_POST ['subscription_t ype'] == "")
{
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 2064
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_r ate'] or $_POST['subscription_t ype'] or
$_POST['first_name'] or $_POST['last_name'] or $_POST['address_street ']
==
"")

should be

if ($_POST['subscription_r ate'] == ""
or $_POST['subscription_t ype'] =="" 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_r ate'] or $_POST['subscription_t ype'] or
$_POST['first_name'] or $_POST['last_name'] or
$_POST['address_street '] == "")


What you are testing for here is

$_POST['subscription_r ate'] contains a value
OR
$_POST['subscription_t ype'] 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_r ate'] == "" or $_POST['subscription_t ype'] ==
"" 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*******@elec trictoolbox.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_r ate'] or $_POST['subscription_t ype'] or
$_POST['first_name'] or $_POST['last_name'] or
$_POST['address_street '] == "")


What you are testing for here is

$_POST['subscription_r ate'] contains a value
OR
$_POST['subscription_t ype'] 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_r ate'] == "" or $_POST['subscription_t ype'] ==
"" 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.cgoc able.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.cgoc able.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
2028
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 no profile picture exist it still trys to display a broken image, Im sure it's a simple mistake any help please? <?php if ($commentUser=="Yes") { ?> <img src="../salute/<?php echo $commentUser;
11
97207
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 have written a theme in python for the superkaramba theme engine on kde (see http://netdragon.sourceforge.net - if you are a kde/linux user, it is a great visual applet engine). I have uploaded it to www.kdelook.org for others to download and...
6
1861
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 great benefit from it? What does ELSE minor-mode for Emacs do that is so great for you? Chris
27
3080
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 few weeks ago while trying to find the best way to form a if-elif-else block, that on a very general level, an 'also' statement might be useful. So I was wondering what others would think of it.
6
61585
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 same structure as follows Keycolumns ========== Material
9
5000
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++ programs with source codes on linux? i.e. It parses any sized C or C++ project to help reverse engineer, document, draw UML diagram and understand it and thus maintain it better.
3
3936
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 right direction about what I am not doing correctly? 1.. Write an If Then Else statement that displays the string "Pontiac" in the CarMakeLabel control if the CarTextBox control contains the string "Grand Am" (in any case).
20
3929
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
2089
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 blank value. If the user fails to enter a number and clicks the UPDATE button, they should get a message that reads "No SSN Entered, Please Enter SSN" Instead they get this message "Match found for " - - ". I'm pulling my hair out over this...
17
2462
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: if($_POST('assign']='Open in Excel')]...{ }elseif($_POST=='Open in Excel')]... ? shouldn't it be else instead of elseif? or should it be
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.