Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    If/Else if seem to both be executing

    Hi there!

    All right, here's the situation: we have a form, and a couple of the drop down menus refresh the page (and actually submit the form) when they've been changed. If any of the other drop downs or fields have been changed, the changes disappear (as they should) when the page refreshes.

    I'm trying to get one of the drop downs selected item to stick around when the page refreshes.

    Here's a chunk of the code:
    PHP Code:
        while ($row $db->fetch_array($result)) {
            echo 
    "<option value=\"$row[0]\" ";
            if (
    $_POST[supporter_id] == $row[0])
            {
                echo 
    "selected";
            }
            else if (
    $info['supporter_id'] == $row[0])
            {
                echo 
    "selected";
            }
            echo 
    "> $row[1] </option>";
        } 
    So... It would make sense that the value coming from $_POST is the most recently selected value. So, I'm trying that one first. Then, if its not, then we'll revert to the original one, coming in from the $info array.

    A weird thing happens when I view the source after the page has been refreshed/submitted - "selected" is echoed both after the newly selected item from the $_POST variable, and after the original item. I can't figure out why.

    Shouldn't the page and form be completely reloading on the refresh, ie. clearing the "selected" off the original item? If something's coming in off the $_POST, then only that part of the if should be executing, right?

    Any thoughts would be greatly appreciated. If I'm not making sense, let me know and I'll try to clarify.

    Thanks,
    Tara

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Try:

    PHP Code:
    while ($row $db->fetch_array($result)) {
            echo 
    "<option value=\"$row[0]\" ";
            if (
    $_POST['supporter_id'] == $row[0])
            {
                echo 
    "selected";
            }
            elseif (
    $info['supporter_id'] == $row[0])
            {
                echo 
    "selected";
            }
            echo 
    "> $row[1] </option>";
        } 
    Dave .... HostMonster for all of your hosting needs

  • #3
    Regular Coder
    Join Date
    Apr 2008
    Location
    New Jersey
    Posts
    116
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You're mixing up levels of logic. Your check against $_POST and against $info happens separately for each record, not once for the entire list. So if you have different values for supporter_id in each, both will get the selected attribute. Probably what you need to do is, before this loop runs, clean up / update the value in $info if there was a value in $_POST.
    Chaos
    Lost Souls: text based RPG | MUDseek: MUD gaming search | MUDfind: MUD resource sites | Discordian Quotes

  • Users who have thanked chaosprime for this post:

    tarad (07-24-2008)

  • #4
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by chaosprime View Post
    You're mixing up levels of logic. Your check against $_POST and against $info happens separately for each record, not once for the entire list. So if you have different values for supporter_id in each, both will get the selected attribute. Probably what you need to do is, before this loop runs, clean up / update the value in $info if there was a value in $_POST.
    Ahhh yes! Thank you so much. You're so smart. Good idea. This worked perfectly. I appreciate it muchly.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •