We help IT Professionals succeed at work.
Get Started

selected="selected" not working

timgreaney
timgreaney asked
on
598 Views
Last Modified: 2013-12-12
I have a form that prepopulates with data entries for a product so that they can be edited and updated.

The text fields' data is prepopulating without a problem using such pieces of code as
<input type="text" name="image" id="image" value="<?php htmlout($image); ?>"/>

however when it comes to a drop down menu I just can't get the menu to select the correct category assigned category. Whatever is selected before the form is submitted updates the listing perfectly (so the <?php htmlout($category['category']); ?> is all good) however as this menu is currently defaulting to the first entry it is a pain having to select the correct category every time. Using echo ' selected="selected"' but its just not working?!

Code for the drop-down is as follows:
<tr><td style="padding:0 0 5px 0;"><label  for="category">Category:</label></td>
<td colspan="3" style="padding:0 0 5px 0;">
                        <select name="category" id="category" style="width:300px;">
                              <option value="">--- Select Category ---</option>
                              <?php foreach ($categorys as $category): ?>
                                    <option value="<?php htmlout($category['category']); ?>"<?php
                                                if ($category['category'] == $category)
                                                      echo ' selected="selected"';
                                                ?>><?php htmlout($category['category']); ?></option>
                              <?php endforeach; ?>
                        </select>
</td></tr>

And this is working alongside the following:

      // Build the list of categories
      $sql = "SELECT DISTINCT category FROM product ORDER BY category ASC";
      $result = mysqli_query($link, $sql);
      if (!$result)
      {
            $error = 'Error fetching category list';
            include 'error.php';
            exit();
      }

      while ($row = mysqli_fetch_array($result))
      {
            $categorys[] = array('id' => $row['id'], 'category' => $row['category']);
      }

Help!
Comment
Watch Question
This problem has been solved!
Unlock 3 Answers and 16 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE