如果使用数组,在下拉列表中获取选定的月份值

时间:2022-12-03 07:49:08

I am trying to get the selected value in the drop down; but the items inside it are in an array. How can I get the value of the selected option?

我试图在下拉菜单中获取所选的值;但是里面的东西都是数组。如何获得所选选项的值?

For now, I have these lines of code:

现在,我有这些代码行:

<?php
// lowest year wanted
$cutoff = 2013;

// current year
$now = date('Y');

// build months menu
echo '<select name="month">' . PHP_EOL;
for ($m=1; $m<=12; $m++) {
    echo '  <option value="' . $m . '">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;

// build days menu
echo '<select name="day">' . PHP_EOL;
for ($d=1; $d<=31; $d++) {
    echo '  <option value="' . $d . '">' . $d . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;

// build years menu
echo '<select name="year">' . PHP_EOL;
for ($y=$now; $y>=$cutoff; $y--) {
    echo '  <option value="' . $y . '">' . $y . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;
?>

Let's say, I picked July for the month. How can I tell the browser that I have chosen July. Any idea?

比方说,我选了7月份。我如何告诉浏览器我选择了7月。任何想法?

3 个解决方案

#1


2  

Try like this

这样的尝试

echo '<select name="month">' . PHP_EOL;
for ($m=1; $m<=12; $m++) {
    if($sel_mnth == $m)
        echo '  <option value="' . $m . '" selected="selected">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
    else   
        echo '  <option value="' . $m . '">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;

or directly

或直接

for ($m=1; $m<=12; $m++) {                   
        echo '  <option value="' . $m . '" if($sel_mnth == $m) echo "selected=\'selected\'";>' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
}

#2


2  

Put your select within a form tag and after submit the form use $_POST['month'] or $_POST['year'] for getting month and year user selected value.

将您的选择放在表单标记中,提交表单后,使用$_POST['month']或$_POST['year']获取月和年用户选择值。

like this

像这样

if (isset($_POST['submit'])){

$year=$_POST['year']; //user selected year value is here
...
}

<form method="POST" action="">
... //here is your select tag codes
...
<input type="submit" name="submit" />
</form>

#3


0  

<select>
    <option value="">--Select Month--</option>
    <?php for ($i=1; $i<=12; $i++)
    {
        ?>
            <option value="<?php echo $i;?>"><?php echo $i;?></option>
        <?php
    } ?>

</select>

#1


2  

Try like this

这样的尝试

echo '<select name="month">' . PHP_EOL;
for ($m=1; $m<=12; $m++) {
    if($sel_mnth == $m)
        echo '  <option value="' . $m . '" selected="selected">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
    else   
        echo '  <option value="' . $m . '">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;

or directly

或直接

for ($m=1; $m<=12; $m++) {                   
        echo '  <option value="' . $m . '" if($sel_mnth == $m) echo "selected=\'selected\'";>' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
}

#2


2  

Put your select within a form tag and after submit the form use $_POST['month'] or $_POST['year'] for getting month and year user selected value.

将您的选择放在表单标记中,提交表单后,使用$_POST['month']或$_POST['year']获取月和年用户选择值。

like this

像这样

if (isset($_POST['submit'])){

$year=$_POST['year']; //user selected year value is here
...
}

<form method="POST" action="">
... //here is your select tag codes
...
<input type="submit" name="submit" />
</form>

#3


0  

<select>
    <option value="">--Select Month--</option>
    <?php for ($i=1; $i<=12; $i++)
    {
        ?>
            <option value="<?php echo $i;?>"><?php echo $i;?></option>
        <?php
    } ?>

</select>