Hi, I have a problem with keep the selected value for dropdown after submit. I have this php code and i don´t know how to retain the selected value after submit It works ok. But, when script made its work I lose values in boxes, previously selected. <form name="search" action="" method="get"><select name="cp_state"> <?php $metakey = 'cp_state'; $counties = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) ); if ($counties) { foreach ($counties as $cp_state) { echo "<option value=\"" . $cp_state . "\">" . $cp_state . "</option>"; $metakey .= ' selected'; } } ?> </select> <input type="submit" value="search" /> </form> Code (markup): Thank you in advance
try this <form name="search" action="" method=""><select name="cp_state"> <?php $metakey = 'cp_state'; $counties = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) ); if ($counties) { foreach ($counties as $cp_state) { echo "<option value=\"" . $cp_state . "\""; if ($_POST['cp_state']==$cp_state) { echo "selected=\"selected\""; } echo ">" . $cp_state . "</option>"; $metakey .= ' selected'; } } ?> </select> <input type="submit" value="search" /> </form> PHP:
You can use selected value in the session and use that value as selected if session variable is exist using isset function