southpawnigeria Posted August 23, 2009 Share Posted August 23, 2009 Hello peeps, I have a drop down menu which i want to stay selected when a value has bn chosen. I have searched through thw different ones but have not found one that supports my coding. Here is the drop down <select name="clients" class="DEPENDS ON form BEING 2" id="clients"> <option value="0" selected="<?php echo $_POST['clients']; ?>">Select Company Name:</option> <?php do { ?> <option value="<?php echo $row_clients['clientsid']?>" selected="<?php echo $_POST['clients']; ?>"><?php echo ucfirst($row_clients['name']);?></option> <?php } while ($row_clients = mysql_fetch_assoc($clients)); $rows = mysql_num_rows($clients); if($rows > 0) { mysql_data_seek($clients, 0); $row_clients = mysql_fetch_assoc($clients); } ?> </select> Please any insights on the issue wud be very appreciated Quote Link to comment https://forums.phpfreaks.com/topic/171509-drop-down-menu-value-should-stay-selected-when-selected/ Share on other sites More sharing options...
Psycho Posted August 23, 2009 Share Posted August 23, 2009 I suggest separating your logic from your HTML. Makes things much easier, IMO. Also, I would revise the variable naming. The select list is currently named "clients", but it is a list to select ONE client, so it should logically be called "client". That way on the processing page you are referncing "$_POST['client']" which makes more sense since you are only expecting one client. This would go in the head or logic file <?php $client_options = ''; while ($client_row = mysql_fetch_assoc($clients)) { $selected = ($client_row['clientsid'] == $_POST['clients']) ? ' selected="selected"': ''; $client_options .= " <option value=\"{$client_row['clientsid']}\"{$selected}>".ucfirst($client_row['name'])."</option>\n"; } ?> This goes in the bottom of the document or in the output script <select name="clients" class="DEPENDS ON form BEING 2" id="clients"> <option value="">Select Company Name:</option> <?php echo $client_options; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/171509-drop-down-menu-value-should-stay-selected-when-selected/#findComment-904474 Share on other sites More sharing options...
southpawnigeria Posted August 24, 2009 Author Share Posted August 24, 2009 It works men, Fits like a glove. A million thnx Quote Link to comment https://forums.phpfreaks.com/topic/171509-drop-down-menu-value-should-stay-selected-when-selected/#findComment-905429 Share on other sites More sharing options...
southpawnigeria Posted August 26, 2009 Author Share Posted August 26, 2009 Oh Oh mj, there is a slight problem with the select menu. It doesnt show the first row in the select menu. It always starts from the second row. Any reason why that is happening. Quote Link to comment https://forums.phpfreaks.com/topic/171509-drop-down-menu-value-should-stay-selected-when-selected/#findComment-906633 Share on other sites More sharing options...
Psycho Posted August 26, 2009 Share Posted August 26, 2009 Of course there is a reason, but I'm not understanding you. Are you saying that the "Select Company Name" is not included in the select list? If so, I don't see how since it is hard coded - unless there is some JavaScript which manipulates that list. The other possible problem that you are trying to describe may be that the 1st company from the database is not included in the select list. If that is the problem, I suspect you implement the code I provided incorrectly and are running a mysql_fetch of some sort before the loop starts - which would "use up" the first record. In any case a more detailed explanation of the problem and a link to the page or the current code would be helpful. Quote Link to comment https://forums.phpfreaks.com/topic/171509-drop-down-menu-value-should-stay-selected-when-selected/#findComment-906672 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.