I would like an item in a dropdown list to be selected if the value corresponds with a value in a database.
Here is my code so far…
$hardware_type = $row['type'];
//pull names from hardware types from databse
$sql = "SELECT * FROM s_num_serialz_types ORDER BY type ASC";
$result = $mysqli->query($sql) or die($mysqli->error.__LINE__);
$type_dropdown = '<select id="serial_type" class="accounts_input_dropdown" type="text" name="serial_type" selected="'.$_POST['serial_type'].'"/><option></option>';
while($row = mysqli_fetch_assoc($result)){
if($row['type'] != $hardware_type){
$type_dropdown .= "\r\n<option value='{$row['type']}'>{$row['type']}</option>";
}else{
$type_dropdown .= "\r\n<option value='{$row['type']} selected=\"selected\">{$row['type']}</option>";
}
}
$type_dropdown .= "\r\n</select>";
I have tried several ways of writing this code and none seem to work. The first time I tried…
while($row = mysqli_fetch_assoc($result)){
if($row['type'] == $hardware_type){
$type_dropdown .= "\r\n<option value='{$row['type']} selected=\"selected\">{$row['type']}</option>";
}else{
$type_dropdown .= "\r\n<option value='{$row['type']}'>{$row['type']}</option>";
}
}
This selected the correct item in the list but the rest of the form containing this list doesn’t show.
Any help will be greatly appreciated
You’ve a typo here
Missing the closing
'. You’re also mixing single and double quotes, which isn’t the best for standards.May i suggest you this compact version: