开发者

Pre-Select an Item based on GET

I to pre select an Item in a 开发者_JAVA百科Listbox based on the value from $_GET.

Ex: Selectbox with options 1,2,3. from the url I get the value 2. How do I preselect this value? The way with <option ...selected>...</> is way too complicated. I just want to declare which option is pre-selected, like <select selected="2">.


The solution with the less number of if statements in my answer is the last one.

Unfortunately, there's no way to so <select selected="2">. You'll have to do something like this:

<select name="select">
  <option value="1"<?php if ( $_GET['select'] == '1' ): ?> selected="selected"<?php endif; ?>>Option 1</option>
  <option value="2"<?php if ( $_GET['select'] == '2' ): ?> selected="selected"<?php endif; ?>>Option 2</option>
  <option value="3"<?php if ( $_GET['select'] == '3' ): ?> selected="selected"<?php endif; ?>>Option 3</option>
</select>

If you allow mutliples, I would use in_array and make sure that the select element's name specifies that it's an array (using the square brackets).

<select multiple="multiple" name="multi_select[]">
  <option value="1"<?php if ( in_array('1', $_GET['multi_select']) ): ?> selected="selected"<?php endif; ?>>Option 1</option>
  <option value="2"<?php if ( in_array('2', $_GET['multi_select']) ): ?> selected="selected"<?php endif; ?>>Option 2</option>
  <option value="3"<?php if ( in_array('3', $_GET['multi_select']) ): ?> selected="selected"<?php endif; ?>>Option 3</option>
</select>

As an alternative, you could use a PHP array to create the options and a foreach loop to create the option.

<select name="select">
<?php

$options = array(
  1 => 'Option 1',
  2 => 'Option 2',
  3 => 'Option 3'
);

foreach ( $options as $value => $name )
{
  echo '<option value="' . $value . '"' . ( $_GET['select'] == $value ? ' selected="selected"' : '' ) . '>' . $name . '</option>';
}

?>
</select>


There is no way to have a selected tag indicate which one is selected. It has to be done through the option tag.

Have you thought about looping through the options?

$desiredSelect = $_GET['desired'];

foreach( array('option-value'  => 'option-text',
               'option-value2' =>'option-text2' ) as $val => $test )
{
    echo "<option value=\"$val\" ";
    if( $val == $desiredSelect ) echo "selected = \"selected\"";
    echo ">$text</option>";
}


An options could be to inject a simple JavaScript/jQuery script to be fired on DOM ready if the request was a get:

if request is get, then inject this code into the response HTML:

$(function(){
    $('#selectBox').val(selectedItemValue);
});

It's that simple. :)


Actually, you have two ways: the first one is to compare each element to selected (as did @Francois Deschenes and @cwallenpoole); the second one is workaround and because of that not very good, but easier to implement. You can use javascript to set selected item to needed after list is formed:

<script type="text/javascript">
$(function() {
    $('select[selected]').val(function() {
        return $(this).attr('selected')
    })
});
</script>
<select selected="7">
    <option value="1">1</option>
    <!-- ... -->
    <option value="10">10</option>
</select>


Another non-if non-js version could possibly be something like this

<?
$res = array($_GET["selected"] => " selected='selected'");    
?>


<select name="select">
  <option value="1"<? echo $res['1']; ?>>Option 1</option>
  <option value="2"<? echo $res['2']; ?>>Option 2</option>
  <option value="3"<? echo $res['3']; ?>>Option 3</option>
</select>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜