在PHP for循环中生成的单选按钮只允许我单击第一个

时间:2014-05-06 13:44:10

标签: php html radio

我正在使用PHP构建预订系统,需要输出一系列可用于以日历格式预订的预订系统。用户只能在表单中选择一次,所以我需要它们作为单选按钮。我已经把它输出了可用的时间很好,但我不能在任何时候选择除第一个单选按钮之外的任何东西。

我认为这可能会有所帮助 - Only one (the first) radio button in a group is selectable / active因为这个想法是一样的,但解决方案是我没有的CSS问题。

下面的$ timeArray是存储的可用时间数组,例如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[]

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>

1 个答案:

答案 0 :(得分:2)

你遗漏了一个属性的引用:

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