testing the condition of a pair of fields

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Davies

    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.="<b r>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.="<b r>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


  • Janwillem Borleffs

    #2
    Re: testing the condition of a pair of fields

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

    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 !=='')) {
    ...
    }
    [color=blue]
    > curiously the following works
    >
    > if(empty($_POST['Link']) || $_POST['Link'] ='' &&
    > empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {
    > $error_msg.="<b r>You didn't select whether to upload a link or a
    > file."; }
    >[/color]

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


    JW



    Comment

    • Ian Davies

      #3
      Re: testing the condition of a pair of fields

      >This way, the condition only applies when both sub-conditions are true
      (both[color=blue]
      >fields are non-empty);[/color]

      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.c om> wrote in message
      news:44a0642c$0 $24649$dbd4b001 @news.euronet.n l...[color=blue]
      > Ian Davies wrote:[color=green]
      > > if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
      > > (isset($_POST['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
      > > $error_msg.="<b r>You can't select to upload a link and a file at the
      > > same time.";
      > >[/color]
      >
      > This way, the condition only applies when both sub-conditions are true[/color]
      (both[color=blue]
      > fields are non-empty); replace the && between the sub-conditions with ||
      >
      > if ((isset($v) && $v !=='') || (isset($v2) && $v2 !=='')) {
      > ...
      > }
      >[color=green]
      > > curiously the following works
      > >
      > > if(empty($_POST['Link']) || $_POST['Link'] ='' &&
      > > empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {
      > > $error_msg.="<b r>You didn't select whether to upload a link or a
      > > file."; }
      > >[/color]
      >
      > That's because with the ||, only one test has to pass. Also note that you
      > are not compatring values (==) but assigning them (=).
      >
      >
      > JW
      >
      >
      >[/color]


      Comment

      • kay

        #4
        Re: testing the condition of a pair of fields


        Ian Davies написав:[color=blue][color=green]
        > >This way, the condition only applies when both sub-conditions are true[/color]
        > (both[color=green]
        > >fields are non-empty);[/color]
        >
        > 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.c om> wrote in message
        > news:44a0642c$0 $24649$dbd4b001 @news.euronet.n l...[color=green]
        > > Ian Davies wrote:[color=darkred]
        > > > if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
        > > > (isset($_POST['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
        > > > $error_msg.="<b r>You can't select to upload a link and a file at the
        > > > same time.";
        > > >[/color]
        > >
        > > This way, the condition only applies when both sub-conditions are true[/color]
        > (both[color=green]
        > > fields are non-empty); replace the && between the sub-conditions with ||
        > >
        > > if ((isset($v) && $v !=='') || (isset($v2) && $v2 !=='')) {
        > > ...
        > > }
        > >[color=darkred]
        > > > curiously the following works
        > > >
        > > > if(empty($_POST['Link']) || $_POST['Link'] ='' &&
        > > > empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {
        > > > $error_msg.="<b r>You didn't select whether to upload a link or a
        > > > file."; }
        > > >[/color]
        > >
        > > That's because with the ||, only one test has to pass. Also note that you
        > > are not compatring values (==) but assigning them (=).
        > >
        > >
        > > JW
        > >
        > >
        > >[/color][/color]

        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?

        Comment

        • Martin Jay

          #5
          Re: testing the condition of a pair of fields

          In message <vQXng.12609$SO 4.3559@newsfe3-win.ntli.net>, Ian Davies
          <iandan.dav@vir gin.net> writes[color=blue]
          >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($_POS T['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
          > $error_msg.="<b r>You can't select to upload a link and a file at the same
          >time.";[/color]

          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.
          [color=blue]
          >curiously the following works
          >
          > if(empty($_POST['Link']) || $_POST['Link'] ='' &&
          >empty($_FILE S['uploadedfile']) || $_FILES['uploadedfile'] ='') {
          > $error_msg.="<b r>You didn't select whether to upload a link or a file.";
          > }[/color]

          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

          Comment

          • Ian Davies

            #6
            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.="<b r>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.="<b r>You can't select to upload a link and a file at the same
            time.";
            }

            error does shows when 'uploadedfile' is empty

            if(!isset($_POS T['Link']) && !isset($_FILES['uploadedfile'])) {
            $error_msg.="<b r>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:APytsyDWNP oEFw7o@spam-free.org.uk...[color=blue]
            > In message <vQXng.12609$SO 4.3559@newsfe3-win.ntli.net>, Ian Davies
            > <iandan.dav@vir gin.net> writes[color=green]
            > >Can anyone tell me why the following code will NOT work
            > >The error message should be displayed when there is something in both[/color][/color]
            fields[color=blue][color=green]
            > >
            > > if ((isset($_POST['Link']) && $_POST['Link'] !=='') &&
            > >(isset($_POS T['uploadedfile']) && $_POST['uploadedfile'] !=='')) {
            > > $error_msg.="<b r>You can't select to upload a link and a file at the[/color][/color]
            same[color=blue][color=green]
            > >time.";[/color]
            >
            > 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.
            >[color=green]
            > >curiously the following works
            > >
            > > if(empty($_POST['Link']) || $_POST['Link'] ='' &&
            > >empty($_FILE S['uploadedfile']) || $_FILES['uploadedfile'] ='') {
            > > $error_msg.="<b r>You didn't select whether to upload a link or a file.";
            > > }[/color]
            >
            > 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[/color]


            Comment

            • Guy

              #7
              Re: testing the condition of a pair of fields

              Ian Davies a écrit :[color=blue]
              > 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.="<b r>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'] ='' &&[/color]
              Hi
              = is not operator; try ==
              [color=blue]
              > empty($_FILES['uploadedfile']) || $_FILES['uploadedfile'] ='') {[/color]

              idem
              G
              [color=blue]
              > $error_msg.="<b r>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
              >
              >[/color]

              Comment

              Working...