简体   繁体   中英

echo selected option in drop down field php

i want echo selected option

global $wpdb;
$results = $wpdb->get_results ("SELECT name FROM wp_tso");


echo '<td><select id="courier_reference" name="courier_reference">';
echo '<option id="courier_reference" name="courier_reference" value="">Select Courier Reference</option>';
foreach ( $results as $result ) {
echo '<option>'.$result->name.'</option>';
}
echo '</select></td>';

this is code its work but if i select option echo Select Courier Reference

now i want

if select echo selected else echo Select Courier Reference

i tried multi ways but not work

thank you

$results = $wpdb->get_results ("SELECT name FROM wp_tso");
$selectedName="previous_selected_name";


echo '<td><select id="courier_reference" name="courier_reference">';
echo '<option id="courier_reference" name="courier_reference" value="">Select Courier Reference</option>';
foreach ( $results as $result ) {
    if ($result->name===$selectedName) {
        echo '<option selected>'.$result->name.'</option>';
    }
    else {
        echo '<option>'.$result->name.'</option>';
    }
}
echo '</select></td>';

Try like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM