我有这个脚本在下拉列表中显示年,月和日。
我的问题是:如何以d-m-y格式获取所选值?
让我们假设我是否有一个按钮来获取当前选定的值并显示在文本框中。我该怎么做?
非常感谢任何想法。谢谢。
<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> ';
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> ';
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> ';
?>
</body>
</html>
答案 0 :(得分:3)
首先,让我纠正你的几个错误:
名为工作日的选择应命名为 day 。这不是强制性的,但选择名称实际上尊重内部数据的含义是一种很好的做法。
如果您要向服务器提交数据,必须包含类似&lt; select&gt;,&lt; input&gt;,&lt; textarea&gt;等元素在一种形式。
<form action="#" method="POST"> [code be here] </form>
应该这样做。
我真的不明白你想要做什么。但是,我已将其缩小为两件事:
您正在尝试将值恢复到服务器,以便进一步处理。我想“在文本框中打印日期”只是一个模拟案例。在这种情况下,您将使用$ _GET / $ _ POST数组(取决于表单使用的方法,请参见上文)。因此,考虑到当前情况,您将拥有:
$selectedDay = $_POST['weekday'];
$selectedMonth = $_POST['month'];
$selectedYear = $_POST['year'];
然后,就像之前说的那样,你会有
<textarea><?php echo $selectedDay."-".$selectedMonth."-".$selectedYear;?></textarea>
(最有可能)您正在尝试在页面上操作而不将值发送回服务器。例如,每次用户在选择中选择不同的值时,脚本都会运行并填充文本框:“您已选择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> ';
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> ';
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> ';
?>
<textarea id="textbox"></textarea>
</body>
</html>
答案 1 :(得分:0)
<textarea>
<?php
echo $currentDay."-".$currentMonth."-".$currentYear;
?>
</textarea>
这将以您在textarea中请求的格式显示日期
答案 2 :(得分: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> ';
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> ';
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> ';
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>