#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    28
    Rep Power
    0

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,604
    Rep Power
    595
    Please enclose your code in [ PHP ] tags. See the sticky at the top of this forum.

    I think you are confusing what happens where. PHP is server side. Once the page is served out that ends PHP's involvement and it knows nothing after that. Drop down selections are made on the browser (client) side so PHP will never see it. You need to accomplish what you want with JavaScript.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    Actually you can use client side or server side language to do this or you can use both (like my example below).

    Code:
    <?php
    $field_country = $_GET["country"];
    ?>
    
    <script>
    function redirectCountry(setCountry)
    {
     setCountry = parseInt(setCountry);
      if (setCountry > 0) {
       document.location.href = "?country=" + setCountry;
      }
    }
    </script>
    
    <dt>Country</dt>
    <dd>
    <select class="select" id="field_country" name="country" onchange="redirectCountry(this.value)">
    <option value="0"/>Select Your Country</option>
    <option value="1">United States</option>
    <option value="2">Canada</option><option value="3">Afghanistan</option>
    </select>
    </dd>
    <?php 
    if ($field_country == 1) {
    echo '<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>';
    } 
    else if ($field_country > 1) {
    echo '<dt>State</dt>
    <dd>
    <select class="select" id="field_state" name="state">
    <option value="6" selected="selected">Outside Usa</option>
    </select>
    </dd>';
    }
    ?>
    It all depends on how you want to display your select menu(s).

IMN logo majestic logo threadwatch logo seochat tools logo