Nabe
Nabe

Reputation: 95

why option selected doesn't comes first

I have created a PHP code to echo the following div

<div id="main_catsel">
<select id="maincat" >

    <option value="1">none</option>
    <option value="2">Computer</option>
    <option value="4">Refrigerator</option>
    <option value="13" selected="selected" >Grinder</option>
    <option value="21">Bed</option>

</select>
</div>

I am using the option values and name as result from query . But now the default result still shown first is according to the id not according to selected="selected". Can any one sugsest a solution for this. Cant remove the id ,because its needed for insertion.

  $maincat .='<div class="main_catsel">';
     $sqlmaincat = $ilance->db->query("SELECT
            cid FROM " . DB_PREFIX . "seller_profile 
            WHERE userid = '" . $uid . "'");
    $row = $ilance->db->fetch_array($sqlmaincat);
    $sqlcat = $ilance->db->query("SELECT
            cid,title_spa 
            FROM " . DB_PREFIX . "categories 
            WHERE parentid = '0'
            ");
         if ($ilance->db->num_rows($sqlcat) > 0)
         {
          $maincat .='<h2><select id="maincat">';
           while ($rows = $ilance->db->fetch_array($sqlcat))
            {  
             if($rows['cid']==$row['cid'])
             {
              $maincat .='<option selected="selected" value="'.$rows['cid'].'">'.$rows['title_spa'].'</option>';
              }
              else
              {
                $maincat .='<option value="'.$rows['cid'].'">'.$rows['title_spa'].'</option>';
                }
             }  
                $maincat .='</select></h2>';
       }
        $maincat .='</div>';

Upvotes: 0

Views: 99

Answers (2)

Mikel
Mikel

Reputation: 25626

What browser are you using?

"On SGML and HTML" points out that some browsers only support the minimized form, i.e. selected rather than selected="selected".

Or maybe you previously selected something, and your browser is remembering your choice. Try holding Shift and clicking the Reload button, or press Ctrl+F5.

Or maybe the your browser is caching an older version of the page. Again Shift+Reload should help. Browser-specific details at Bypass your cache.

Upvotes: 2

Sreeni Puthiyillam
Sreeni Puthiyillam

Reputation: 485


It seems that you just do like this
$("select#maincat").val("13");

Thats it. just try this and Let me know further.

Upvotes: 0

Related Questions