Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:21:03+00:00 2026-06-11T17:21:03+00:00

I would like an item in a dropdown list to be selected if the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:21:05+00:00Added an answer on June 11, 2026 at 5:21 pm

    You’ve a typo here

    <option value='{$row['type']}
    

    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:

    while($row = mysqli_fetch_assoc($result)){
        $selected = ($row['type'] == $hardware_type) ? " selected='selected'" : '';
        $type_dropdown .= "\r\n<option value='{$row['type']}'$selected>{$row['type']}</option>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to keep an item selected, on a ListView, when the user
I would like to get the first item from a list matching a condition.
I would like to get collection or list of Item objects, but I get
I would like to display an editable list of items, each item of which
I would like to know which item in select list was last clicked I
I would like to change the dropdown list and whenever I make some changes
I would like to show a select-dropdown list in a jQuery Popup / Dialog.
I have a dropdown list, what I would like to do is assign values
I would like to add an item into a picture Library using c#. This
I would like to extend the team explorer work item editor with a custom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.