如何知道选择哪个选项PHP

时间:2016-03-02 10:31:01

标签: php arrays date

你好我有一个选择,我有一些选项,它由PHP中的日期数组函数生成但我想知道哪一个被选中所以当我使用它后我可以检查哪一个被选中所以我可以执行动作在它上面

<select>

    <?php
    function getDays( $year, $startMonth = 1, $startDay = 1, $dayOfWeek = 'monday' ) {
        $start = new DateTime( sprintf( '%04d-%02d-%02d', $year, $startMonth, $startDay ) );
        $start->modify( $dayOfWeek );
        $end = new DateTime( sprintf( '%04d-12-31', $year ) );
        $end->modify( '+1 day' );
        $interval = new DateInterval( 'P1W' );
        $period = new DatePeriod( $start, $interval, $end );

        foreach ( $period as $dt ) {

            echo "<option value=";
            echo $dt->format( "d/m/Y" );
            echo '">';

        echo $dt->format( "d/m/Y" );
        echo "</option>";


    }
    }

    getDays( 2016, 2, 12, 'monday' );
    ?>
</select> 

1 个答案:

答案 0 :(得分:0)

<?php

(isset($_POST["company"])) ? $company = $_POST["company"] : $company=1;

?>

<form>
<select id="company" name="company">
<option <?php if ($company == 1 ) echo 'selected' ; ?> value="1">Apple</option>
<option <?php if ($company == 2 ) echo 'selected' ; ?> value="2">Samsung</option>
<option <?php if ($company == 3 ) echo 'selected' ; ?> value="3">HTC</option>
</select>
</form>