If ... else that does not work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Blow

    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


  • IWP506@gmail.com

    #2
    Re: If ... else that does not work

    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

    Comment

    • Chris Hope

      #3
      Re: If ... else that does not work

      Joe Blow wrote:
      [color=blue]
      > 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 '] == "")[/color]

      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

      Comment

      • Joe Blow

        #4
        Re: If ... else that does not work


        "Chris Hope" <blackhole@elec trictoolbox.com > wrote in message
        news:d6j44b$moi $1@lust.ihug.co .nz...[color=blue]
        > Joe Blow wrote:
        >[color=green]
        >> 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 '] == "")[/color]
        >
        > 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[/color]

        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


        Comment

        • Geoff Berrow

          #5
          Re: If ... else that does not work

          I noticed that Message-ID: <neaje.4549$Rr3 .236@read1.cgoc able.net> from
          Joe Blow contained the following:
          [color=blue]
          >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.[/color]

          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/

          Comment

          • Chris Hope

            #6
            Re: If ... else that does not work

            Geoff Berrow wrote:
            [color=blue]
            > I noticed that Message-ID: <neaje.4549$Rr3 .236@read1.cgoc able.net>
            > from Joe Blow contained the following:
            >[color=green]
            >>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.[/color]
            >
            > 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.[/color]

            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

            Comment

            Working...