ok i have a drop down box (combo box) on a page with two options hard coded into it. What i want to be able to do is when a link is clicked on the page before that takes the user to that page i want one of the options in the drop down box to auto select.

Anyone have any ideas on how to do this? i guess you would need to use a $_get but how do you slect what option you want??

any help would be great

cheers!

    the html is:

    <option value="0" selected="selected" >pick me</option>

    so you need to write the php to add selected="selected" when appropriate.

      On the initial page the link would have to contain a GET variable, just like you stated i.e.

      <a href='page2.php?Option=1'>Link - Option 1</a>
      <a href='page2.php?Option=2'>Link - Option 2</a>
      

      Then on the processing page (page2.php) you would generate the select box and add the following if statements:

      <select name='MySelect'>
      <?php
      if ( isset($_GET['Option']) && $_GET['Option'] == 1)
      {
          echo "<option value=''>Select option</option>";
          echo "<option value='Value1' selected>Option 1</option>";
          echo "<option value='Value2'>Option 1</option>";
      }
      else if ( isset($_GET['Option']) && $_GET['Option'] == 2)
      {
          echo "<option value=''>Select option</option>";
          echo "<option value='Value1'>Option 1</option>";
          echo "<option value='Value2' selected>Option 1</option>";
      }
      else
      {
          echo "<option value=''>Select option</option>";
          echo "<option value='Value1'>Option 1</option>";
          echo "<option value='Value2'>Option 1</option>";
      }
      ?>
      </select>

      If you up the number of possible options in the dropdown I would probably recommend using an array of possible values. Then as you iterate through the array, if it matches just the text 'Selected' to the option.

      Hope this helps...

        wow that's total over kill, even without an array (the best option) it can be done in 3 lines

        <option value=''>Select option</option>
        <option <?php echo $_GET['selected'] == '1'?' selcted="selected"':''?>value='Value1'>Option 1</option>
        <option <?php echo $_GET['selected'] == '2'?' selcted="selected"':''?>value='Value2'>Option 2</option>
        

          What you have suggested would be fine if the GET variable is populated. If it is not you will get a number of undefined index errors all over the page.

          <?php $selected = isset($_GET['selected']) ? $_GET['selected'] : ""; ?>
          <option value=''>Select option</option>
          <option <?php echo $selected == '1' ? 'selcted="selected"':''?>value='Value1'>Option 1</option>
          <option <?php echo $selected == '2' ? 'selected="selected"':''?>value='Value2'>Option 2</option>

          This would capture the error.

            <select name='MySelect'> 
            <?php 
            $array = array(
            	'1' => 'value a',
            	'2' => 'value b',
            	'3' => 'value c',
            	'4' => 'value d',
            	'5' => 'value e',
            );
            
            $value = (isset($_GET['value'])) ? $_GET['value'] : '';
            
            foreach ($array as $k => $v) {
            	$s = ($value == $k) ? " selected" : '';
            	echo "<option value='{$k}'{$s}>{$v}</option>\n"; 
            }
            ?> 
            </select>
            
              [uk]stuff;10899164 wrote:

              What you have suggested would be fine if the GET variable is populated. If it is not you will get a number of undefined index errors all over the page.

              notices not errors, yeah there is a difference, that's why i turn them of :-)

                dagon wrote:

                notices not errors, yeah there is a difference, that's why i turn them of :-)

                True, but "warnings" - a.k.a. non-fatal runtime errors - are still a type of error, so there are some who wish further refine their coding habits to eliminate both fatal and non-fatal errors.

                  [uk]stuff;10899164 wrote:

                  What you have suggested would be fine if the GET variable is populated. If it is not you will get a number of undefined index errors all over the page.

                  <?php $selected = isset($_GET['selected']) ? $_GET['selected'] : ""; ?>
                  <option value=''>Select option</option>
                  <option <?php echo $selected == '1' ? 'selcted="selected"':''?>value='Value1'>Option 1</option>
                  <option <?php echo $selected == '2' ? 'selected="selected"':''?>value='Value2'>Option 2</option>

                  This would capture the error.

                  This to me is the best option. You wont get the error due to the ternary statement and it only uses 1 extra line of code (other than the output code:

                  <?php echo $selected == '2' ? 'selected="selected"':''?>

                    thanks for your input everyone. I will be trying this later today. i will let you know how i get on with it 🙂

                      farius;10899219 wrote:

                      <?php $selected = isset($GET['selected']) ? $GET['selected'] : ""; ?>
                      <option value=''>Select option</option>
                      <option <?php echo $selected == '1' ? 'selcted="selected"':''?>value='Value1'>Option 1</option>
                      <option <?php echo $selected == '2' ? 'selected="selected"':''?>value='Value2'>Option 2</option> QUOTE]

                      This seemed to work spot on. If any one would like to walk me through exactly what each bit of the code is doing id be greatful. I think i get the gist but like to make sure when learning 😉

                        Write a Reply...