If statements when submitting form

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

    If statements when submitting form

    I'm setting up a form and want to check if some conditions are met when
    the form submits. If they aren't, the form displays a missing field
    message and the user has to go back to fill in the missing data. If it
    is met, the form continues processing. I have the $state variable coming
    from a drop down SELECT option with "" being the SELECTED default.

    The if statements are included inside the form tags on the form page,
    not on the processing page.

    <?php
    if (isset($state)) { //if state is set, require the following fields
    ?>
    <input type="hidden" name="require"
    value="name,add ress,city,state ,zipcode,phone, email,question" >
    <?php
    }
    if (!isset($state) ) { //if state is not set, require the following fields
    ?>
    <input type="hidden" name="require"
    value="name,add ress,city,count ry,zipcode,phon e,email,questio n">
    <?php
    }

    These both work properly. But where I'm running into trouble is when I
    want to further define the if statement to contain like:

    if ((!isset($state )) && ($country == "USA")) {
    ?>
    <input type="hidden" name="require"
    value="name,add ress,city,state ,zipcode,phone, email,question" >
    <?php
    }

    This doesn't work. It will allow the empty $state and the value "USA" in
    $country to submit. How can I prevent this?

  • Pedro Graca

    #2
    Re: If statements when submitting form

    Jack wrote:
    ....[color=blue]
    > This doesn't work. It will allow the empty $state and the value "USA" in
    > $country to submit. How can I prevent this?[/color]

    Put this inside the second if()

    if (isset($state)) { /* whatever */ }
    if (!isset($state) ) {
    if ($country == "USA") {
    /* whatever */
    } else {
    /* whatever */
    }
    }


    --
    USENET would be a better place if everybody read: | to email me: use |
    http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
    http://www.netmeister.org/news/learn2quote2.html | header, textonly |
    http://www.expita.com/nomime.html | no attachments. |

    Comment

    • Pjotr Wedersteers

      #3
      Re: If statements when submitting form

      Pedro Graca wrote:[color=blue]
      > Jack wrote:
      > ...[color=green]
      >> This doesn't work. It will allow the empty $state and the value
      >> "USA" in $country to submit. How can I prevent this?[/color]
      >
      > Put this inside the second if()
      >
      > if (isset($state)) { /* whatever */ }
      > if (!isset($state) ) {
      > if ($country == "USA") {
      > /* whatever */
      > } else {
      > /* whatever */
      > }
      > }[/color]

      I agree the solution you give is simpler, but can't see _why_ the original
      won't work. Can you point it out?
      Always having a hard time with boolean logic...


      Comment

      • Pedro Graca

        #4
        Re: If statements when submitting form

        Pjotr Wedersteers wrote:[color=blue]
        > Pedro Graca wrote:[color=green]
        >> Put this inside the second if()
        >>
        >> if (isset($state)) { /* whatever */ }
        >> if (!isset($state) ) {
        >> if ($country == "USA") {
        >> /* whatever */
        >> } else {
        >> /* whatever */
        >> }
        >> }[/color]
        >
        > I agree the solution you give is simpler, but can't see _why_ the original
        > won't work. Can you point it out?
        > Always having a hard time with boolean logic...
        >
        >[/color]

        Original code simplified, reformatted, and numbered:
        1 <?php
        2 if (isset($state)) { echo 'a'; }
        3 if (!isset($state) ) { echo 'b'; }
        4 if ((!isset($state )) && ($country == "USA")) { echo 'c'; }
        5 ?>


        When isset($state) is false this script will echo 'b' (line 3) no matter
        what is the value of $country.

        If, $country is 'USA' and $state is not set, the script will echo 'c'
        (line 4), right after having echoed 'b';

        The tests for
        isset($state)
        and
        $country == 'USA'
        are independent, but should be tested together.


        --
        USENET would be a better place if everybody read: | to email me: use |
        http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
        http://www.netmeister.org/news/learn2quote2.html | header, textonly |
        http://www.expita.com/nomime.html | no attachments. |

        Comment

        • For example John Smith

          #5
          Re: If statements when submitting form

          Pedro Graca wrote:[color=blue]
          > Pjotr Wedersteers wrote:[color=green]
          >> Pedro Graca wrote:[color=darkred]
          >>> Put this inside the second if()
          >>>
          >>> if (isset($state)) { /* whatever */ }
          >>> if (!isset($state) ) {
          >>> if ($country == "USA") {
          >>> /* whatever */
          >>> } else {
          >>> /* whatever */
          >>> }
          >>> }[/color]
          >>
          >> I agree the solution you give is simpler, but can't see _why_ the
          >> original won't work. Can you point it out?
          >> Always having a hard time with boolean logic...
          >>
          >>[/color]
          >
          > Original code simplified, reformatted, and numbered:
          > 1 <?php
          > 2 if (isset($state)) { echo 'a'; }
          > 3 if (!isset($state) ) { echo 'b'; }
          > 4 if ((!isset($state )) && ($country == "USA")) { echo 'c'; }
          > 5 ?>
          >
          >
          > When isset($state) is false this script will echo 'b' (line 3) no
          > matter what is the value of $country.
          >
          > If, $country is 'USA' and $state is not set, the script will echo 'c'
          > (line 4), right after having echoed 'b';
          >
          > The tests for
          > isset($state)
          > and
          > $country == 'USA'
          > are independent, but should be tested together.[/color]
          Of course! It must have been a bad day, I missed this... Tsss. Thanks Pedro!


          Comment

          • Jack

            #6
            Re: If statements when submitting form

            Pedro Graca wrote:[color=blue]
            > Pjotr Wedersteers wrote:
            >[color=green]
            >>Pedro Graca wrote:
            >>[color=darkred]
            >>>Put this inside the second if()
            >>>
            >>>if (isset($state)) { /* whatever */ }
            >>>if (!isset($state) ) {
            >>> if ($country == "USA") {
            >>> /* whatever */
            >>> } else {
            >>> /* whatever */
            >>> }
            >>>}[/color]
            >>
            >>I agree the solution you give is simpler, but can't see _why_ the original
            >>won't work. Can you point it out?
            >>Always having a hard time with boolean logic...
            >>
            >>[/color]
            >
            >
            > Original code simplified, reformatted, and numbered:
            > 1 <?php
            > 2 if (isset($state)) { echo 'a'; }
            > 3 if (!isset($state) ) { echo 'b'; }
            > 4 if ((!isset($state )) && ($country == "USA")) { echo 'c'; }
            > 5 ?>
            >
            >
            > When isset($state) is false this script will echo 'b' (line 3) no matter
            > what is the value of $country.
            >
            > If, $country is 'USA' and $state is not set, the script will echo 'c'
            > (line 4), right after having echoed 'b';
            >
            > The tests for
            > isset($state)
            > and
            > $country == 'USA'
            > are independent, but should be tested together.[/color]

            Thank you Pedro. While it's great to have someone write a working code
            to correct my mistake, it's always nice to see an explanation of why
            something I wrote did or didn't work. Seeing this was a big help in
            understanding where I went wrong.

            Jack

            Comment

            Working...