upperbid Posted June 30, 2009 Share Posted June 30, 2009 I am trying to make it so I can select a specific number in the code below. It creates a dropdown menu from 1 to 99, but I don't know how to code it so that it shows with a specific number selected like say "7". Any help would be appreciated. print '</select><b>.</b><select name="taxpersf" type="text">'; $st = 0; $st1 = 0; $st2 = 0; while ($st2 <= 99) { print '<option value="'. $st++.'" selected_'.$st1++.'>'.$st2++.'</option>'; print " "; } Quote Link to comment https://forums.phpfreaks.com/topic/164182-solved-need-help-selecting-a-specific-number-in-a-numerical-dropdown-menu/ Share on other sites More sharing options...
taquitosensei Posted June 30, 2009 Share Posted June 30, 2009 This is how you set the selected option <option value='7' selected='selected'>7</option> Quote Link to comment https://forums.phpfreaks.com/topic/164182-solved-need-help-selecting-a-specific-number-in-a-numerical-dropdown-menu/#findComment-866065 Share on other sites More sharing options...
HPWebSolutions Posted June 30, 2009 Share Posted June 30, 2009 Hi Upperbid, Can you tell me what selected_'.$st1++.' is for, I think it may be incorrect here. If you want it to display the value the user selected before submitting the form, you would do something like this: (note, if you are using the "get" action instead of "post", replace $_POST WITH $_GET) print '</select><b>.</b><select name="taxpersf" type="text">'; $st = 0; $st1 = 0; $st2 = 0; while ($st2 <= 99) { print '<option value="'. $st++.'" selected_'.$st1++.' '.($_POST['taxpersf']==($st)?"selected='selected'":"").'>'.$st2++.'</option>'; print " "; } Quote Link to comment https://forums.phpfreaks.com/topic/164182-solved-need-help-selecting-a-specific-number-in-a-numerical-dropdown-menu/#findComment-866069 Share on other sites More sharing options...
HPWebSolutions Posted June 30, 2009 Share Posted June 30, 2009 I should point out, in case you aren't familiar with it, the format (logical_expression?value1:value2) is called the ternary expression. If logical_expression evaluates to true, value1 will be the output of the statement, otherwise if false then value2 will be the output. Quote Link to comment https://forums.phpfreaks.com/topic/164182-solved-need-help-selecting-a-specific-number-in-a-numerical-dropdown-menu/#findComment-866074 Share on other sites More sharing options...
upperbid Posted June 30, 2009 Author Share Posted June 30, 2009 Thanks everyone for their help so far, but none of the answers approach the problem. The code already works for displaying the drop-down menu so there is nothing wrong with the code I provided. Also, I know how to show a selected option in HTML, that is basic HTML stuff. My problem, is I'm trying to insert a selected option in the existing code. It currently prints out a drop-down menu from 0 to 99. How do I add code to the code I provided to make it show a specified number such as 7 selected when the page is loaded? Thanks for any help provided. NOTE: The Selected_ in the code is simply part of the value of each number and has nothing to do with the problem (it makes the value "Selected_1", "Selected_2", etc.) and is not relevant to the issue. It can just as well be Number_ and makes no difference. Quote Link to comment https://forums.phpfreaks.com/topic/164182-solved-need-help-selecting-a-specific-number-in-a-numerical-dropdown-menu/#findComment-866094 Share on other sites More sharing options...
abdfahim Posted June 30, 2009 Share Posted June 30, 2009 print '</select><b>.</b><select name="taxpersf" type="text">'; $st = 0; $st1 = 0; $st2 = 0; while ($st2 <= 99) { print '<option value="'. $st++.'" selected_'.$st1++; if($st2==7){ print ' selected'; } print '>'.$st2++.'</option>'; print " "; } Quote Link to comment https://forums.phpfreaks.com/topic/164182-solved-need-help-selecting-a-specific-number-in-a-numerical-dropdown-menu/#findComment-866101 Share on other sites More sharing options...
upperbid Posted July 1, 2009 Author Share Posted July 1, 2009 That worked, thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/164182-solved-need-help-selecting-a-specific-number-in-a-numerical-dropdown-menu/#findComment-866906 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.