php - SELECTED not working with <option> tag inside a for loop/ while loop -
i want 1 of options in drop down selected default, please see code
<?php class html{ function output(){ $html='<td>'.'<select id="out">'; for($i=0;$i<21;$i++){ $html.='<option value="$i" if($i==5) { selected } >'. $i .'</option>'; } return $html; } } echo html::output(); ?>
here want value 5 selected default,but getting selected value 20. thank you!!
you're line incorrect. use instead:
$html .= '<option value="' . $i . '"' . ( $i==5 ? ' selected="selected"' : '' ) . '>' . $i . '</option>';
i'm making use of ternary comparison operator.
Comments
Post a Comment