0

我有这个脚本,它在下拉列表中显示年、月和日。

我的问题是:如何获取 dmy 格式的选定值?

假设我是否有一个按钮来获取当前选定的值并显示在文本框上。我该怎么做?

任何想法都非常感谢。谢谢你。

<html>
    <body>
<?php
    $months = array(
            'January', 
            'February', 
            'March', 
            'April', 
            'May', 
            'June',
            'July', 
            'August', 
            'September', 
            'October', 
            'November',
            'December'
        );
    $weekday = array(
            'Sunday',
            'Monday',
            'Tuesday',
            'Wednesday',
            'Thursday',
            'Friday',
            'Saturday'
        );
    $days = range(1,31);
    $years = range (2010, 2020);

    $currentDay = date('d');
    $currentMonth = date('F');
    $currentYear = date('Y');

    echo "<select name='weekday'>";
    foreach($days as $valued) {

        if($valued == $currentDay)
        { 
            $default = 'selected="selected"'; 
            echo '<option '.$default.' value="'.$valued.'">'.$valued.'</option>\n';
        } else {
            $default='';
            echo '<option '.$default.' value="'.$valued.'">'.$valued.'</option>\n';
        }
        } 
    echo '</select>&nbsp;';
        echo "<select name='month'>";
        foreach($months as $valuem) {
            if($valuem==$currentMonth)
        { 
            $default1 = 'selected="selected"';
            echo '<option '.$default1.' value="'.$valuem.'">'.$valuem.'</option>\n';
        } else {
            $default1 = '';    
            echo '<option '.$default1.' value="'.$valuem.'">'.$valuem.'</option>\n';
        }

    } 
        echo '</select> &nbsp; ';
        echo "<select name='year'>";

        foreach ($years as $valuey) {

    if($valuey == $currentYear)
    { 
       $default2 = 'selected="selected"';
       echo '<option '.$default2.' value="'.$valuey.'">'.$valuey.'</option>\n';
    } else {
           $default2 = '';
           echo '<option '.$default2.' value="'.$valuey.'">'.$valuey.'</option>\n';
    }

        }  
        echo '</select> &nbsp; ';
?>


</body>
</html>
4

3 回答 3

3

首先,让我纠正你几个错误:

  1. 名为weekday的选择应命名为day。这不是强制性的,但是选择的名称实际上尊重内部数据的含义是一种很好的做法。

  2. 如果要向服务器提交数据,则必须在表单中包含 <select>、<input>、<textarea> 等元素。

<form action="#" method="POST"> [code be here] </form>

应该做。

我真的不明白你想做什么。但是,我将其缩小到两件事:

  1. 您正在尝试将值取回服务器,以进行进一步处理。我想“在文本框中打印日期”只是一个模型案例。在这种情况下,您将使用 $_GET/$_POST 数组(取决于表单使用的方法,见上文)。因此,考虑到当前情况,您将拥有:

    $selectedDay = $_POST['weekday'];
    $selectedMonth = $_POST['month'];
    $selectedYear = $_POST['year'];
    

    然后,就像之前所说的那样,你会有

    <textarea><?php echo $selectedDay."-".$selectedMonth."-".$selectedYear;?></textarea>
    
  2. (很可能)您正在尝试在页面上操作而不将值发送回服务器。例如,每次用户在选择中选择不同的值时,都会运行一个脚本并在一个文本框中填充:“您已选择 23-2-1992”。在这种情况下,您不使用 PHP,而是使用 Javascript。

这是您的代码,就是这样做的(重构):

<html>
<head>
<script type="text/javascript">
function updateTextBox()
{
    day = document.getElementById("day").value;
    aux = document.getElementById("month");

    month = aux.value;
    //Use the following if you want the month number instead of the month name, e.g. 12-4-2012
    //month = aux.options[aux.selectedIndex].id;

    year = document.getElementById("year").value;

    document.getElementById("textbox").value = day + "-" + month + "-" + year;
}
</script>
<title>Some title here</title>
</head>
<body>

<?php
$months = array ('January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November','December');
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range(1,31);
$years = range (2010, 2020);

$currentDay = date('d');
$currentMonth = date('F');
$currentYear = date('Y');

echo "<select name='day' id='day' onChange='updateTextBox()'>";
foreach($days as $valued) {

    if($valued == $currentDay)
    { 
          $default = 'selected="selected"'; 
            echo '<option '.$default.' value="'.$valued.'">'.$valued.'</option>\n';
    } else {
          $default='';
            echo '<option '.$default.' value="'.$valued.'">'.$valued.'</option>\n';
    }

} 
echo '</select>&nbsp;';


echo "<select name='month' id='month' onChange='updateTextBox()'>";

foreach($months as $array_key=>$valuem) {

    if($valuem==$currentMonth)
    { 
        $default1 = 'selected="selected"';
    } else {
        $default1 = '';
    }
    echo '<option '.$default1.' id="'.($array_key+1).'" value="'.$valuem.'">'.$valuem.'</option>\n';

} echo '</select> &nbsp; ';

echo "<select name='year' id='year' onChange='updateTextBox()'>";

foreach ($years as $valuey) {

        if($valuey == $currentYear)
        { 
            $default2 = 'selected="selected"';
        } else {
            $default2 = '';
        }
        echo '<option '.$default2.' value="'.$valuey.'">'.$valuey.'</option>\n';

}
echo '</select> &nbsp; ';
?>

<textarea id="textbox"></textarea>

</body>
</html>
于 2012-09-19T08:34:45.910 回答
0
<textarea>

<?php

echo $currentDay."-".$currentMonth."-".$currentYear;

?>
</textarea>

这将以您在文本区域中请求的格式显示日期

于 2012-09-19T07:38:51.567 回答
0
<?php
if(isset($_POST['submit'])){
    $message=$_POST['weekday'].'-'.$_POST['month'].'-'.$_POST['year'];
}
?>
<html>
<body>
<?php
$months = array(
        'January', 
        'February', 
        'March', 
        'April', 
        'May', 
        'June',
        'July', 
        'August', 
        'September', 
        'October', 
        'November',
        'December'
    );
$weekday = array(
        'Sunday',
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday'
    );
$days = range(1,31);
$years = range (2010, 2020);

$currentDay = date('d');
$currentMonth = date('F');
$currentYear = date('Y');
echo '<form method="post" action="">';
echo "<select name='weekday'>";
foreach($days as $valued) {

    if($valued == $currentDay)
    { 
        $default = 'selected="selected"'; 
        echo '<option '.$default.' value="'.$valued.'">'.$valued.'</option>\n';
    } else {
        $default='';
        echo '<option '.$default.' value="'.$valued.'">'.$valued.'</option>\n';
    }
    } 
echo '</select>&nbsp;';
    echo "<select name='month'>";
    foreach($months as $valuem) {
        if($valuem==$currentMonth)
    { 
        $default1 = 'selected="selected"';
        echo '<option '.$default1.' value="'.$valuem.'">'.$valuem.'</option>\n';
    } else {
        $default1 = '';    
        echo '<option '.$default1.' value="'.$valuem.'">'.$valuem.'</option>\n';
    }

} 
    echo '</select> &nbsp; ';
    echo "<select name='year'>";

    foreach ($years as $valuey) {

if($valuey == $currentYear)
{ 
   $default2 = 'selected="selected"';
   echo '<option '.$default2.' value="'.$valuey.'">'.$valuey.'</option>\n';
} else {
       $default2 = '';
       echo '<option '.$default2.' value="'.$valuey.'">'.$valuey.'</option>\n';
}

    }  
    echo '</select> &nbsp; ';
    echo '<input type="submit" name="submit" id="submit" value="Get Selected Date">          </input></form>';
?>
<p>You Select:</p>
<input type="text" value="<?php if(isset($message)){echo $message;}  ?>"></input>

</body>
</html>
于 2012-09-19T08:48:40.160 回答