2017-04-10 82 views
0

我需要在按下前往按鈕上顯示選定月份和日期的日期。從兩個相應的下拉列表中選擇月份和年份的日期和日期

我試過REQUESTGET從URL,onchange()得到它,但我肯定做錯了什麼。我知道,當前的代碼僅是2003年

<table> 
<tr> 
    <form id="frmA" class="form-inline" role="form" > 
     <td align="right"> 
      <select id="month" name="month" class="form-control" required="required"> 
       <option value="">Select Month...</option> 
       <option value='1'>January</option> 
       <option value='2'>February</option> 
       <option value='3'>March</option> 
       <option value='4'>April</option> 
       <option value='5'>May</option> 
       <option value='6'>June</option> 
       <option value='7'>July</option> 
       <option value='8'>August</option> 
       <option value='9'>September</option> 
       <option value='10'>October</option> 
       <option value='11'>November</option> 
       <option value='12'>December</option> 
      </select> 
     </td> 
     <td> 
      <select id="year" name="year" class="form-control" required="required"> 
      <?php 
       foreach (range(date('Y'), $earliest_year) as $x) { 
       print '<option value="'.$x.'"'.($x === $already_selected_value ? ' 
       selected="selected"' : '').'>'.$x.'</option>';} 
       ?> 
      </select> 
     </td> 
     <td> 
      <input type="submit" value="Go!" /> 
     </td> 
    </form> 
</tr> 
<table> 
    <tbody> 
     <?php 
      $date = '2003-09-01'; 
      $end = '2003-09-' . date('t', strtotime($date)); 
      i=1; 
      while(strtotime($date) <= strtotime($end) && $i <= strtotime($end)) { 
       $day_num = date("d/m/Y", strtotime($date)); 
       $day_name = date('l', strtotime($date)); 
       $date = date("Y-m-d", strtotime("+1 day", strtotime($date))); 
       echo "<tr><td>$day_num - $day_name</td></tr>"; 
       i++; 
      } 
      ?> 
    </tbody> 
</table> 
+0

你沒有在你的代碼的幾件事情:1 /方法+在「形式」使用他們前行的動作2 /分配$瓦爾 - > $日期=「2003-09-01 「;檢查此示例的代碼與未成年人更正[鏈接](https://pastebin.com/u0ty2g45)編輯:不是一個好的東西把表格內TR/TD恕我直言 – OldPadawan

+0

感謝您的及時答覆。我剛開始學習php,並會記住你的建議(形式在tr/td內)。再次感謝(Y) –

+0

很高興幫助,如果是這樣,請接受答案;) – OldPadawan

回答

0

9個月的靜態和值顯示你缺乏代碼中的幾件事情:1 /方法+在「形式」 2 /分配$瓦爾行動在線使用它們之前 - > $ date ='2003-09-01';

<table> 
<tbody> 
<?php 

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

$month = $_POST['month']; 
$year = $_POST['year']; 

$date = "$year-$month-01"; 
$end = "$year-$month-" . date('t', strtotime($date)); 

echo"[ $month/$year/$date/$end ]"; 
/* code below will throw an error 
$i=1; 
while((strtotime($date) <= strtotime($end)) && ($i <= strtotime($end))  ) { 
$day_num = date("d/m/Y", strtotime($date)); 
$day_name = date('l', strtotime($date)); 
$date = date("Y-m-d", strtotime("+1 day", strtotime($date))); 
echo "<tr><td>$day_num - $day_name</td></tr>"; 
i++; 
} 
*/ 
?> 
</tbody> 
</table> 

<table> 
<tr> 
<form id="frmA" class="form-inline" role="form" method="post" action="mypage.php" > 
<td align="right"> 

<select id="month" name="month" class="form-control" required="required"> 
        <option value="">Select Month...</option> 
        <option value='1'>January</option> 
        <option value='2'>February</option> 
        <option value='3'>March</option> 
        <option value='4'>April</option> 
        <option value='5'>May</option> 
        <option value='6'>June</option> 
        <option value='7'>July</option> 
        <option value='8'>August</option> 
        <option value='9'>September</option> 
        <option value='10'>October</option> 
        <option value='11'>November</option> 
        <option value='12'>December</option> 
    </select> 
</td> 

<td> 
<select id="year" name="year" class="form-control" required="required"> 
<?php 
foreach (range(date('Y'), $earliest_year) as $x) { 
print '<option value="'.$x.'"'.($x === $already_selected_value ? ' 
selected="selected"' : '').'>'.$x.'</option>';} 
?> 
</select> 
</td> 

<td> 
<input type="submit" value="Go!" /> 
</td> 
</form> 
</tr></tbody> 
</table> 
+0

表單不允許爲表,tbody或tr的子元素。嘗試放置一個會導致瀏覽器將表單移動到表格後面(同時保留其內容 - 表格行,表格單元格,輸入等)。 –

+0

*你的代碼中缺少一些東西:1 /一個方法*假如表單總是有一個方法,即使你沒有寫一個方法 –

+0

@MasivuyeCokile:但是它更加乾淨的代碼放一個,不是嗎說?和+1,因爲我們都提到了/ html的形式:) – OldPadawan

0
<table> 
<tbody> 
<?php 

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

$month = $_REQUEST['month']; 
$year = $_REQUEST['year']; 

$date = "$year-$month-01"; 
$end = "$year-$month-" . date('t', strtotime($date)); 

$i=1; 
while((strtotime($date) <= strtotime($end)) && ($i <= strtotime($end)) ) { 
$day_num = date("d/m/Y", strtotime($date)); 
$day_name = date('l', strtotime($date)); 
$date = date("Y-m-d", strtotime("+1 day", strtotime($date))); 
echo "<tr><td>$day_num - $day_name</td></tr>"; 
$i++; 
} 
?> 
</tbody> 
</table> 
<form id="frmA" class="form-inline" role="form" method="post" action="mypage.php" > 
<table> 
<tr> 
<td align="right"> 

<select id="month" name="month" class="form-control" required="required"> 
        <option value="">Select Month...</option> 
        <option value='1'>January</option> 
        <option value='2'>February</option> 
        <option value='3'>March</option> 
        <option value='4'>April</option> 
        <option value='5'>May</option> 
        <option value='6'>June</option> 
        <option value='7'>July</option> 
        <option value='8'>August</option> 
        <option value='9'>September</option> 
        <option value='10'>October</option> 
        <option value='11'>November</option> 
        <option value='12'>December</option> 
</select> 
</td> 

<td> 
<select id="year" name="year" class="form-control" required="required"> 
<?php 
foreach (range(date('Y'), $earliest_year) as $x) { 
print '<option value="'.$x.'"'.($x === $already_selected_value ?'selected="selected"' : '').'>'.$x.'</option>';} 
?> 
</select> 
</td> 

<td> 
<input type="submit" value="Go!" /> 
</td> 

</tr> 
</table> 
</form> 
相關問題