testing the condition of a pair of fields

This is a discussion on testing the condition of a pair of fields within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi Can anyone tell me why the following code will NOT work The error message should be displayed when there ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-26-2006
Ian Davies
 
Posts: n/a
Default testing the condition of a pair of fields

Hi

Can anyone tell me why the following code will NOT work
The error message should be displayed when there is something in both fields

if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
(isset($_POST['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
$error_msg.="<br>You can't select to upload a link and a file at the same
time.";

curiously the following works

if(empty($_POST['Link']) || $_POST['Link'] ='' &&
empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {
$error_msg.="<br>You didn't select whether to upload a link or a file.";
}

to display a message when there is nothing in both fields

help gratefuly requested
Ian


Reply With Quote
  #2 (permalink)  
Old 06-26-2006
Janwillem Borleffs
 
Posts: n/a
Default Re: testing the condition of a pair of fields

Ian Davies wrote:
> if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
> (isset($_POST['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
> $error_msg.="<br>You can't select to upload a link and a file at the
> same time.";
>


This way, the condition only applies when both sub-conditions are true (both
fields are non-empty); replace the && between the sub-conditions with ||

if ((isset($v) && $v !=='') || (isset($v2) && $v2 !=='')) {
...
}

> curiously the following works
>
> if(empty($_POST['Link']) || $_POST['Link'] ='' &&
> empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {
> $error_msg.="<br>You didn't select whether to upload a link or a
> file."; }
>


That's because with the ||, only one test has to pass. Also note that you
are not compatring values (==) but assigning them (=).


JW



Reply With Quote
  #3 (permalink)  
Old 06-27-2006
Ian Davies
 
Posts: n/a
Default Re: testing the condition of a pair of fields

>This way, the condition only applies when both sub-conditions are true
(both
>fields are non-empty);


Thats exactly what I want only one field should have something in it and
both fields should NOT be empty at the same time. But it doesnt work.

Ian


"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:44a0642c$0$24649$dbd4b001@news.euronet.nl...
> Ian Davies wrote:
> > if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
> > (isset($_POST['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
> > $error_msg.="<br>You can't select to upload a link and a file at the
> > same time.";
> >

>
> This way, the condition only applies when both sub-conditions are true

(both
> fields are non-empty); replace the && between the sub-conditions with ||
>
> if ((isset($v) && $v !=='') || (isset($v2) && $v2 !=='')) {
> ...
> }
>
> > curiously the following works
> >
> > if(empty($_POST['Link']) || $_POST['Link'] ='' &&
> > empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {
> > $error_msg.="<br>You didn't select whether to upload a link or a
> > file."; }
> >

>
> That's because with the ||, only one test has to pass. Also note that you
> are not compatring values (==) but assigning them (=).
>
>
> JW
>
>
>



Reply With Quote
  #4 (permalink)  
Old 06-27-2006
kay
 
Posts: n/a
Default Re: testing the condition of a pair of fields


Ian Davies написав:
> >This way, the condition only applies when both sub-conditions are true

> (both
> >fields are non-empty);

>
> Thats exactly what I want only one field should have something in it and
> both fields should NOT be empty at the same time. But it doesnt work.
>
> Ian
>
>
> "Janwillem Borleffs" <jw@jwscripts.com> wrote in message
> news:44a0642c$0$24649$dbd4b001@news.euronet.nl...
> > Ian Davies wrote:
> > > if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
> > > (isset($_POST['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
> > > $error_msg.="<br>You can't select to upload a link and a file at the
> > > same time.";
> > >

> >
> > This way, the condition only applies when both sub-conditions are true

> (both
> > fields are non-empty); replace the && between the sub-conditions with ||
> >
> > if ((isset($v) && $v !=='') || (isset($v2) && $v2 !=='')) {
> > ...
> > }
> >
> > > curiously the following works
> > >
> > > if(empty($_POST['Link']) || $_POST['Link'] ='' &&
> > > empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {
> > > $error_msg.="<br>You didn't select whether to upload a link or a
> > > file."; }
> > >

> >
> > That's because with the ||, only one test has to pass. Also note that you
> > are not compatring values (==) but assigning them (=).
> >
> >
> > JW
> >
> >
> >


The boolean result of initialization the variables in 'if' condition
is always 'true' so the code like

if ( (...||...||...||$a=''||...) && (...||...||...||$b=''||...)&&
..... (...||...||...||$z=''||...))

will be always true as Janwillem Borleffs mentioned.


Maybe you meant $Post['Link']=='' in your code?

Reply With Quote
  #5 (permalink)  
Old 06-27-2006
Martin Jay
 
Posts: n/a
Default Re: testing the condition of a pair of fields

In message <vQXng.12609$SO4.3559@newsfe3-win.ntli.net>, Ian Davies
<iandan.dav@virgin.net> writes
>Can anyone tell me why the following code will NOT work
>The error message should be displayed when there is something in both fields
>
> if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
>(isset($_POST['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
> $error_msg.="<br>You can't select to upload a link and a file at the same
>time.";


Here you're using $_POST['uploadedfile']. I'd guess it should probably
be $_FILES['uploadedfile']['name'], which is the name of the file that
was uploaded.

>curiously the following works
>
> if(empty($_POST['Link']) || $_POST['Link'] ='' &&
>empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {
> $error_msg.="<br>You didn't select whether to upload a link or a file.";
> }


Here you're using $_FILES['uploadedfile'], which would be an array.

And $_POST['Link'] ='' will always be true because it is giving
$_POST['Link'] the value ''.
--
Martin Jay
Phone/SMS: +44 7740 191877
Fax: +44 870 915 2124
Reply With Quote
  #6 (permalink)  
Old 06-27-2006
Ian Davies
 
Posts: n/a
Default Re: testing the condition of a pair of fields

Yes there was syntax errors in my previous post but i tried every
combination even with the correct syntax. The problem was the

$_FILES['uploadedfile']

which wasnt being recognised as a field
I found eventually that it should be

$_FILES['uploadedfile'] [Name]

now everything is fine
Thanks for the help

Ian



should be
if($_POST['Link']!=='' && $_FILES['uploadedfile']!=='') {
$error_msg.="<br>You can't select to upload a link and a file at the same
time.";
}

error does shows when 'uploadedfile' is empty

if($_POST['Link']<>'' && $_FILES['uploadedfile']<>'') {
$error_msg.="<br>You can't select to upload a link and a file at the same
time.";
}

error does shows when 'uploadedfile' is empty

if(!isset($_POST['Link']) && !isset($_FILES['uploadedfile'])) {
$error_msg.="<br>You can't select to upload a link and a file at the same
time.";
}

error does not show even when both fields have something in them
error does not show

"Martin Jay" <martin@spam-free.org.uk> wrote in message
news:APytsyDWNPoEFw7o@spam-free.org.uk...
> In message <vQXng.12609$SO4.3559@newsfe3-win.ntli.net>, Ian Davies
> <iandan.dav@virgin.net> writes
> >Can anyone tell me why the following code will NOT work
> >The error message should be displayed when there is something in both

fields
> >
> > if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
> >(isset($_POST['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
> > $error_msg.="<br>You can't select to upload a link and a file at the

same
> >time.";

>
> Here you're using $_POST['uploadedfile']. I'd guess it should probably
> be $_FILES['uploadedfile']['name'], which is the name of the file that
> was uploaded.
>
> >curiously the following works
> >
> > if(empty($_POST['Link']) || $_POST['Link'] ='' &&
> >empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {
> > $error_msg.="<br>You didn't select whether to upload a link or a file.";
> > }

>
> Here you're using $_FILES['uploadedfile'], which would be an array.
>
> And $_POST['Link'] ='' will always be true because it is giving
> $_POST['Link'] the value ''.
> --
> Martin Jay
> Phone/SMS: +44 7740 191877
> Fax: +44 870 915 2124



Reply With Quote
  #7 (permalink)  
Old 06-27-2006
Guy
 
Posts: n/a
Default Re: testing the condition of a pair of fields

Ian Davies a crit :
> Hi
>
> Can anyone tell me why the following code will NOT work
> The error message should be displayed when there is something in both fields
>
> if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
> (isset($_POST['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
> $error_msg.="<br>You can't select to upload a link and a file at the same
> time.";
>
> curiously the following works
>
> if(empty($_POST['Link']) || $_POST['Link'] ='' &&

Hi
= is not operator; try ==

> empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {


idem
G

> $error_msg.="<br>You didn't select whether to upload a link or a file.";
> }
>
> to display a message when there is nothing in both fields
>
> help gratefuly requested
> Ian
>
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:20 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0