2014-05-06 42 views
-2

我正在使用PHP構建預訂系統,並且需要輸出可用於以日曆格式預訂的時間範圍。用戶只能在表單中選擇一次,所以我需要它們是單選按鈕。我已經準備好了輸出可用時間,但是在任何時候我都不能選擇除第一個單選按鈕之外的其他任何東西。在PHP for循環中生成的單選按鈕只會讓我點擊第一個按鈕

我認爲這可能會有幫助 - Only one (the first) radio button in a group is selectable/active由於想法是一樣的,但解決方案是一個我沒有的CSS問題。

$ timeArray below是存儲的可用時間的數組,例如2014-05-05-01,2014-05-05-02,2014-05-06-01。每個元素中的最後兩位數字指的是當天的時間段。

<?php 
for($i=0;$i<count($timeArray); $i++){ ?> 
<input type="radio" name="request" id="request<?php echo $timeArray[$i]; ?>" value="<?php echo $timeArray[$i];?>" /> 
<?php } ?> 

它輸出一系列時間和單選按鈕很好,但我只能選擇日曆中每一天的第一個單選按鈕。

由於只有一個來自整個表單的值,所以我留下了這個名字,就像'請求'一樣。這是正確的嗎?我試過它作爲

request[] 

request[$i] 

這沒有什麼區別,而且也試過它作爲

request[<?php echo $timeArray[$i]; ?>] 

這讓所有的按鈕可以選擇作爲他們都有不同的名字然後。

請您指點我正確的方向嗎?

這是日曆代碼:

<table width="100%" align="center"> 
         <tr valign="baseline"> 
          <td valign="top" nowrap="nowrap" align="center"><?php 
    //set variables to today 
     $day = date('d'); 
     $month = date('m'); 
     $year = date('Y'); 

     $monthCount = 0; 

    for($monthCount = 0; $monthCount <3; $monthCount++){ 
    if ($month <= (date('m')+1)){ 
    //first day of the month 
    $first_day = mktime(0,0,0,$month, 1, $year) ; 

    //month name 
    $title = date('F', $first_day) ; 
    $day_of_week = date('D', $first_day) ; 

    //determine how many blank days needed before the first day of the month 
    switch($day_of_week){ 
    case "Sun": $blank = 0; break; 
    case "Mon": $blank = 1; break; 
    case "Tue": $blank = 2; break; 
    case "Wed": $blank = 3; break; 
    case "Thu": $blank = 4; break; 
    case "Fri": $blank = 5; break; 
    case "Sat": $blank = 6; break; 
    } 

    //how many days in the current month 
    $days_in_month = cal_days_in_month(0, $month, $year) ; 

          <table border="1" class='scalendar'> 
           <tr> 
           <th colspan = "7"> <?php echo $title;?>&nbsp;<?php echo $year; ?></th> 
           </tr> 
           <tr class='scalendar'> 
           <td>Sun</td> 
           <td>Mon</td> 
           <td>Tue</td> 
           <td>Wed</td> 
           <td>Thu</td> 
           <td>Fri</td> 
           <td>Sat</td> 
           </tr> 
           <?php 
    $day_count = 1; 
    ?> 
           <tr > 
           <?php 
      while ($blank > 0) 
      { ?> 
           <td></td> 
           <?php 
      $blank = $blank-1; 
      $day_count++; 
      } 
      $day_num = 1; 
      while ($day_num <= $days_in_month) 
      { 
      ?> 
           <td valign="top"><label> <span class="main"><?php echo $day_num; ?></span><br /> 
            <?php 
      //code displays months < 10 as single digit numbers - correct this with the following 
      if(strlen($month) == 1){ 
       $month = "0".$month; 
       } 
      //code displays days < 10 as single digit numbers - correct this with the following 
      if(strlen($day_num) == 1){ 
       $day_num = "0".$day_num; 
       } 
if(count($timeArray) > 0){ 
    for($i=0;$i<count($timeArray); $i++){ 
<input type="radio" name="request" id="request<?php echo $timeArray[$i]; ?>" value="<?php echo $timeArray[$i];?>" /> 
echo "<br>"; 
} //end of for loop 
} // end of if array length is > 0 loop 
if(count($timeArray) == 0){ 
      //today has no availability ?> 
            - 
            <?php } ?> 
            </label></td> 
           <?php $day_num++; 
      $day_count++; 

      //new row every week 
      if ($day_count > 7) 
      { 
      ?> 
           </tr> 
           <tr> 
           <?php 
    $day_count = 1; 
    } 
    } 
    while ($day_count >1 && $day_count <=7) 
    { 
    ?> 
           <td></td> 
           <?php 
    $day_count++; 
    }//end of while day count is > 1 and <=7 
    $month = $month+1; 
    if($month > 12){ 
    $year = $year+1; 
    $month = 1; 
    } 
    echo "<br />"; 
    }}//end of while month is less than current month +2 
    ?> 
           </tr> 
           <tr></tr> 
          </table> 

這是表單生成的HTML代碼:

<form action="request.php?" method="post" name="form2" id="form2"> 
        <tr> 
        <td class="main"><table width="100%" align="center"> 
         <tr valign="baseline"> 
          <td valign="top" nowrap="nowrap" align="center">        <table border="1" class='scalendar'> 
           <tr> 
           <th colspan = "7"> May&nbsp;2014</th> 
           </tr> 
           <tr class='scalendar'> 
           <td>Sun</td> 
           <td>Mon</td> 
           <td>Tue</td> 
           <td>Wed</td> 
           <td>Thu</td> 
           <td>Fri</td> 
           <td>Sat</td> 
           </tr> 
                   <tr > 
                    <td></td> 
                    <td></td> 
                    <td></td> 
                    <td></td> 
                    <td valign="top"><label> <span class="main">1</span><br /> 
            09:15 - 09:30       <input type="radio" name="request" id="request2014-05-01-2" value="2014-05-01-2" /> 
         <br>09:30 - 09:45       <input type="radio" name="request" id="request2014-05-01-3" value="2014-05-01-3" /> 
         <br>10:00 - 10:15       <input type="radio" name="request" id="request2014-05-01-5" value="2014-05-01-5" /> 
         <br>10:15 - 10:30       <input type="radio" name="request" id="request2014-05-01-6" value="2014-05-01-6" /> 
         <br>11:45 - 12:00       <input type="radio" name="request" id="request2014-05-01-12" value="2014-05-01-12" /> 
         <br>                    </label></td> 
                    <td valign="top"><label> <span class="main">2</span><br /> 
            09:00 - 09:15       <input type="radio" name="request" id="request2014-05-02-1" value="2014-05-02-1" /> 
         <br>10:00 - 10:15       <input type="radio" name="request" id="request2014-05-02-5" value="2014-05-02-5" /> 
         <br>10:30 - 10:45       <input type="radio" name="request" id="request2014-05-02-7" value="2014-05-02-7" /> 
         <br>11:00 - 11:15       <input type="radio" name="request" id="request2014-05-02-9" value="2014-05-02-9" /> 
         <br>11:15 - 11:30       <input type="radio" name="request" id="request2014-05-02-10" value="2014-05-02-10" /> 
         <br>11:30 - 11:45       <input type="radio" name="request" id="request2014-05-02-11" value="2014-05-02-11" /> 
         <br>                    </label></td> 
                    <td valign="top"><label> <span class="main">3</span><br /> 
                      - 
                      </label></td> 
                    </tr> 
           <tr> 
                    <td valign="top"><label> <span class="main">4</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">5</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">6</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">7</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">8</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">9</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">10</span><br /> 
                      - 
                      </label></td> 
                    </tr> 
           <tr> 
                    <td valign="top"><label> <span class="main">11</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">12</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">13</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">14</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">15</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">16</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">17</span><br /> 
                      - 
                      </label></td> 
                    </tr> 
           <tr> 
                    <td valign="top"><label> <span class="main">18</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">19</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">20</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">21</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">22</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">23</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">24</span><br /> 
                      - 
                      </label></td> 
                    </tr> 
           <tr> 
                    <td valign="top"><label> <span class="main">25</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">26</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">27</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">28</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">29</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">30</span><br /> 
                      - 
                      </label></td> 
                    <td valign="top"><label> <span class="main">31</span><br /> 
                      - 
                      </label></td> 
                    </tr> 
           <tr> 
           <br />         </tr> 
           <tr></tr> 
          </table></td> 
         </tr> 
         <tr valign="baseline"> 
          <td valign="top" nowrap="nowrap"></td> 
         </tr> 
         <tr valign="baseline"> 
          <td valign="top" nowrap="nowrap"><div align="center"> 
           <input name="btnSubmit" type="submit" class="main" id="btnSubmit" value="Request" /> 
           <input type="hidden" name="MM_insert" value="form2" /> 
          </div></td> 
         </tr> 
         <tr valign="baseline"> 
          <td valign="top" nowrap="nowrap">&nbsp;</td> 
         </tr> 
         </table></td> 
        </tr> 
       </form> 
+0

你能告訴我們生成的HTML嗎? –

+0

我不確定你的意思,抱歉聽起來很愚蠢。使用單選按鈕的HTML的唯一一點是我上面寫的。你的意思是日曆嗎?我之前已經使用過許多日曆代碼,這個單選按鈕是唯一不同的東西,這就是爲什麼我認爲是這樣的原因。 – jaggy

+0

是的 - 如果我們能看到導致問題產生的相關標記,我們就可以更容易地診斷問題。 –

回答

2

你遺漏了引號的attruibutes之一的周圍:

<input type="radio" name="request" id="request<?php echo $timeArray[$i]; ?> value="<?php echo $timeArray[$i];?> 
                     ^^^^^^ 
                      HERE 
+0

錯過了 - 謝謝。我修改了我的代碼,並在上面的解釋中,但我仍然只能點擊第一個單選按鈕。 – jaggy

相關問題