kvnirvana Posted May 17, 2010 Share Posted May 17, 2010 How can i make the ‘all’ selection in the drop down list to be non selectable and greyed out but still shown as the first option when the user opens the page with the dropdowns? /*------------------------------------------------------------------------ create the drop downs ------------------------------------------------------------------------*/ function dropdown($field, $table) { //initialize variables $oHTML = ''; $result = ''; //check to see if the field is passed correctly if (($field == "")||($table == "")) { die("No column or table specified to create drop down from!"); } $sql = "select distinct($field) from $table"; //call the db function and run the query $result = conn($sql); //if no results are found to create a drop down return a textbox if ((!$result) ||(mysql_num_rows($result)==0)) { $oHTML .= "<input type='text' name='$field' value='' size='15'>"; }elseif (($result)&&(mysql_num_rows($result)>0)){ //build the select box out of the results $oHTML .= "<select name='$field'>\n<option value='all'>All</option>\n"; while ($rows = mysql_fetch_array($result)) { $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; } $oHTML .= "</select>\n"; } //send the value back to the calling code return $oHTML; }//end function Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted May 17, 2010 Share Posted May 17, 2010 Just add the disabled attribute to the option you want disabled. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 17, 2010 Author Share Posted May 17, 2010 Tried with this $oHTML .= "<select name='$field'>\n<option disabled='disabled'>All</option>\n"; But it doesn't work Quote Link to comment Share on other sites More sharing options...
haku Posted May 17, 2010 Share Posted May 17, 2010 You can disable the whole select element, but you cannot disable a single option. The best you can do is add the option, then have a check in the background that sends back an error if the user selects that option. There is no way to prevent an option from being selected though. Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted May 17, 2010 Share Posted May 17, 2010 Sorry about that, i just skimmed over your question. Haku is correct that you cannot disable a single element. I would just give the option a value of zero, or ''. Then, I'd firstly make sure that the value>0 or value!='' when the user is submitting to form, via javascript, and secondly make the same check in php, since a user can disable javascript, and get by your validation. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 17, 2010 Author Share Posted May 17, 2010 Ok thanks for the replies, but how can I do it if I wanted to do it the way haku suggested? Quote Link to comment Share on other sites More sharing options...
haku Posted May 17, 2010 Share Posted May 17, 2010 That's a PHP question (or whatever server-side language you are using), and isn't really applicable to this thread - you should really open a new thread for that. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 17, 2010 Author Share Posted May 17, 2010 I posted this in php help, but someone moved it to this forum. Quote Link to comment Share on other sites More sharing options...
haku Posted May 17, 2010 Share Posted May 17, 2010 That's because your original question was an HTML question. That question was answered - now you're on to a PHP question. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.