Jump to content

[SOLVED] Need Help Selecting a specific number in a numerical dropdown menu


Recommended Posts

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 " ";
}

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 " ";
    } 

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.

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.

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 " ";

  }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.