简体   繁体   中英

create a dropdown list for month and year and selected in php

I have a "search by month and year " feature and have listed the months and year in a drop-down (select) field.so far this is working perfect but i need to show selected (month and year) after submitting the form .but i unable to do how to get selected year and month in drop down.below is my php JavaScript code .anyone help me how to solve

below is the two dropdowns for month year .the month and year sholud be shown as selected.

<select name="month" class="form-control" >

<?php

for ($i = 0; $i <= 12; ++$i)
    {
    $time = strtotime(sprintf('+%d months', $i));
    $label = date('F ', $time);
    $value = date('m', $time);
    printf('<option value="%s" selected="selected">%s</option>', $value, $label);
    }

?>
</select>
<select name="year" class="form-control">
      <?php

for ($i = 0; $i <= 12; ++$i)
    {
    $time = strtotime(sprintf('-%d years', $i));
    $value = date('Y', $time);
    $label = date('Y ', $time);
    printf('<option value="%s" selected="selected">%s</option>', $value, $label);
    }

?>
</select>

so far this is working perfect but i need to show selected (month and year) after submitting the form .but i unable to do how to get selected year and month in drop down.below is my php java-script code .anyone help me how to solve it?

Try this code

<select name="month" class="form-control" >
<?php
    for ($i = 1; $i <= 12; ++$i){
        $time = strtotime(sprintf('+%d months', $i));
        $label = date('F ', $time);
        $value = date('m', $time);
        echo '<option value="'.$value.'" ';
        if((isset($_GET['month']))&&($value==$_GET['month']))echo 'selected';// Check if form submitted or not. select the month if yes
        echo '>'.$label.'</option>';
    }
?>
</select>
<select name="year" class="form-control">
<?php
    for ($i = 0; $i <= 12; ++$i){
        $time = strtotime(sprintf('-%d years', $i));
        $value = date('Y', $time);
        echo '<option value="'.$value.'" ';
        if((isset($_GET['year']))&&($value==$_GET['year']))echo 'selected';// Check if form submitted or not. select the year if yes
        echo '>'.$value.'</option>';
    }
?>
</select>

You should be able to use the value held in the $_REQUEST array to find the value of each select menu. However as you have stated the form uses GET it is preferable to then use values from the $_GET array as this leaves the POST method free and will not affect the selected values should there be any POSTing of data ( ajax etc )

<!doctype html>
<html>
    <head>
        <meta charset='utf-8' />
        <title></title>
    </head>
    <body>
        <form>
            <select name="month" class="form-control">
                <?php
                    $month = !empty( $_GET['month'] ) ? $_GET['month'] : 0;
                    for ($i = 0; $i <= 12; ++$i) {
                        $time = strtotime(sprintf('+%d months', $i));
                        $label = date('F ', $time);
                        $value = date('m', $time);

                        $selected = ( $value==$month ) ? ' selected=true' : '';

                        printf('<option value="%s"%s>%s</option>', $value, $selected, $label );
                    }
                ?>
            </select>

            <select name="year" class="form-control">
                <?php

                    $year = !empty( $_GET['year'] ) ? $_GET['year'] : 0;

                    for ($i = 0; $i <= 12; ++$i)  {
                        $time = strtotime(sprintf('-%d years', $i));
                        $value = date('Y', $time);
                        $label = date('Y ', $time);

                        $selected = ( $value==$year ) ? ' selected=true' : '';

                        printf('<option value="%s"%s>%s</option>', $value, $selected, $label);
                    }
                ?>
            </select>
            <input type='submit' />
        </form>
    </body>
</html>

For month code

for ($i = 0; $i <= 12; ++$i){
    $time = strtotime(sprintf('+%d months', $i));
    $label = date('F ', $time);
    $value = date('m', $time);
    $sleceted='';
    if(isset($_REQUEST['month']){
        $sleceted=$_REQUEST['month']== $value?'selected:selected':'';
    }
    printf('<option value="%s" '.$sleceted.'>%s</option>', $value, $label);
}

for year code

for ($i = 0; $i <= 12; ++$i){
    $time = strtotime(sprintf('+%d months', $i));
    $label = date('F ', $time);
    $value = date('m', $time);
    $sleceted='';
    if(isset($_REQUEST['year']){
        $sleceted=$_REQUEST['year']== $value?'selected:selected':'';
    }
    printf('<option value="%s" '.$sleceted.'>%s</option>', $value, $label);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM