PHP显示标签和选定的值

时间:2013-12-03 06:58:16

标签: php post

::: EDIT :::

好的,显然我应该在这里放大图,这是我的错。好吧,让我们说有两组下拉列表,

考虑以下HTML:

<form name="myform" action="process.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
<select name="month">
<option selected="--">--</option>
<option value="January">01</option>
<option value="February">02</option>
<option value="March">03</option>
<option value="April">04</option>
<option value="May">05</option>
<option value="June">06</option>
<option value="July">07</option>
<option value="August">08</option>
<option value="September">09</option>
<option value="October">10</option>
<option value="November">11</option>
<option value="December">12</option>
</select>
<select name="year">
<option selected="--">--</option>
<option value="1965">65</option>
<option value="1966">66</option>
<option value="1967">67</option>
<option value="1968">68</option>
</select>
<input type="submit" />
</form>

我在'process.php'中使用$ _POST来显示用户选择的年份,如下所示:

<?php
 echo "{$_POST['month']} ";
 echo "{$_POST['year']} ";
 ?>

如果选择“67”,则显示“1967”并且当选择“09”时,显示“九月”,所以问题仍然存在......

有没有办法可以在同一页面上显示选项和标签?这是带有PHP处理器的简单HTML,仅此而已。

谢谢!

3 个答案:

答案 0 :(得分:1)

你无法通过php实现它,你可以使用jquery,如下面的

   <select name="month" onchange="document.getElementById('month_text').value=this.options[this.selectedIndex].text">
     <option selected="--">--</option>
     <option value="January">01</option>
     <option value="February">02</option>
     <option value="March">03</option>
      ............
     <option value="December">12</option>
   </select>

   <select name="year" onchange="document.getElementById('year_text').value=this.options[this.selectedIndex].text">
      <option selected="--">--</option>
      <option value="1965">65</option>
      <option value="1966">66</option>
      <option value="1967">67</option>
      <option value="1968">68</option>
  </select>

    <input type="hidden" name="year_text" id="year_text" value="" />
    <input type="hidden" name="month_text" id="month_text" value=""/>
php中的

  echo $_POST['year']; // will print 1967
  echo $_POST['year_text']; // will print 67

  echo $_POST['month']; // will print January
  echo $_POST['month_text']; // will print 01

答案 1 :(得分:0)

<?php
 $monthArray = array('01'=>'January','02'=>'February','03'=>'March','04'=>'April',
                     '05'=>'May','06'=>'June','07'=>'July','08'=>'August',
                     '09'=>'September','10'=>'October','11'=>'November',
                     '12'=>'December');
 $month = $_POST['month'];
 $year = $_POST['year'];
 echo $month;
 echo $year;
 echo "You have selected ".substr($year, 2)." Year.";
 echo "You have selected ".$monthArray[$month]." month.";
?>

我已经更新了月份的代码。

答案 2 :(得分:0)

你可以这样做: 修改html,如:

<option value="1965-65">65</option>

在PHP中:

$explode = explode("-",$_POST['year']);
echo $explode[0]; //will be 1965
echo $explode[1]; //will be 65

OR 你的HTML

<option value="1965">65</option>

PHP

echo substr($_POST['year'], 2, 2);  // 65