www.webdeveloper.com
Results 1 to 1 of 1

Thread: [RESOLVED] Get selected value in dropdown list

  1. #1
    Join Date
    May 2010
    Posts
    24

    resolved [RESOLVED] Get selected value in dropdown list

    Hi,

    I have a [WordPress] site where a selected option is saved to the database (no array).
    I want to have this value returned from the database in a new dropdown list.

    So:
    HTML Code:
    <select name="user_location" id="user_location">
    	<option value="1">1</option>
    	<option value="2">2</option>
    </select>
    This is the dropdown that saves the selected value to the database.

    Here I want to return the selected value in a dropdown list with the same options when saved.
    However I would like the saved value from before to be selected in the dropdown.
    So I have the same form:
    HTML Code:
    <select name="user_location" id="user_location">
    	<option value="1">1</option>
    	<option value="2">2</option>
    </select>
    and some PHP to get the selected value:
    PHP Code:
    <?php the_author_meta'user_location'$current_user->ID ); ?>
    But I can't seem to match them to what I want.

    It's probably something very simple.....
    Any idea's? Google isn't very helpfull, or I'm just working with the wrong search words

    M.

    -EDIT-
    Unbelievable:
    If anyone wonders....
    I searched wrong and yes it was the obvious
    First: Get the current value from the database
    Second: Check where value compares with the possible options and add the 'selected' attribute
    Code:
    <?php $selected = get_the_author_meta( 'user_location', $current_user->ID );?>
    <select name="user_location" id="user_location">
    	<option <?php if ($selected == '1' ) echo 'selected'; ?> value="1">1</option>
    	<option <?php if ($selected == '2' ) echo 'selected'; ?> value="2">2</option>
    </select>
    Last edited by Interactive1984; 06-12-2014 at 04:16 AM. Reason: Solved

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles