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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP option selection help

    Hello,

    I tried to create a drop-down for users to pick. If a user pick 'United States', then Us States will show up. Otherwise, 'Outside usa' will show.

    It didn't work. Here is my code:

    dt>Country</dt>
    <dd>
    <select class="select" id="field_country" name="country">
    <option value="1" selected="selected">United States</option>
    <option value="2">Canada</option><option value="3">Afghanistan</option>
    </select>
    </dd>
    <?php if (field_country) == "1") { ?>
    <dt>State</dt>
    <dd>
    <select class="select" id="field_state" name="state">
    <option value="1">Alabama</option>
    <option value="2">Alaska</option>
    <option value="3">Arizona</option>
    <option value="5" selected="selected">California</option>
    </select>
    </dd>
    <?php } else { ?>
    <dt>State</dt>
    <dd>
    <select class="select" id="field_state" name="state">
    <option value="6" selected="selected">Outside Usa</option>
    </select>
    </dd>
    <?php } ?>

    Thanks.

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    333
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Check how you've written your variable - You've forgotten the $:

    PHP Code:
    <?php if (field_country) == "1") { ?>
    <dt>State</dt>
    Should be:

    PHP Code:
    <?php if ($field_country) == "1") { ?>
    <dt>State</dt>

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This is incorrect as well. That should be:
    PHP Code:
    <?php if ($field_country == 1) { ?>
    Without the closing ) there. 1 and "1" are considered the same values with the equality check.
    Just a heads up though, this of course will not change what is actually shown upon selection. You need to use JS for that. As you have it right now, it will show a selected "United States", followed by a field for the "Outside USA" option. It would be better to split them up to force one, then force another. Can't do a whole lot code wise since we don't know what your form's are doing, but you'd need to self post it back to get that data.
    PHP Code:
    <dt>Country</dt>
    <dd>
    <select class="select" id="field_country" name="country">
    <option value="1" selected="selected">United States</option>
    <option value="2">Canada</option><option value="3">Afghanistan</option>
    </select>
    </dd>
    <dt>State</dt>
    <dd>
    <select class="select" id="field_state" name="state">
    <?php
    if (!isset($_POST['field_country']) || isset($_POST['field_country']) && $_POST['field_country'] == 1)
    {
    ?>
        <option value="1">Alabama</option>
        <option value="2">Alaska</option>
        <option value="3">Arizona</option>
        <option value="5" selected="selected">California</option>
    <?php
    }
    else
    {
        print 
    '<option value="6" selected="selected">Outside USA</option>';
    }
    ?>
    </select>
    </dd>
    Like that. That way on first load it will assume no post has been send back to indicate the first options, then the second will take the !isset check and give the states. It can be much better written though, PHP wise you may be better off never presenting the options until its been selected, then walk the form through. AJAX wise it can be done on the fly.


  •  

    Posting Permissions

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