将$_Get与单选按钮和下拉列表一起使用


Using $_Get with Radio Buttons and Drop Down list

我已经成功地将我的数据以url:的形式发送到我的页面

http://localhost:8101/Tutorials/sandbox/myfiles/make_a_booking.php?period=2&date=04/29/2014&room=028

但我很难将数据放入表格中。但我该如何将句点=2放入单选按钮,将room=028放入选择下拉菜单?

<form class="submit_date" method="post" action="insert.php" id="submit_date" onsubmit="return confirm('Confirm your booking?');">           
    <p>Date: <input type="text" name="datepicker" id="datepicker" required value="<?php echo $_GET['date'];?>"></p>
    <input type="radio" name="bookperiod" id="bookperiod" value="1" required/>1<br>
    <input type="radio" name="bookperiod" id="bookperiod" value="2" required/>2<br>
    <input type="radio" name="bookperiod" id="bookperiod" value="3" required/>3<br>    
    <input type="radio" name="bookperiod" id="bookperiod" value="4" required/>4<br>    
    <input type="radio" name="bookperiod" id="bookperiod" value="5" required/>5<br>
    <select class="dropdown" id="bookroom" name="bookroom" required;>
    <option selected disabled hidden value=''></option>
    <?php
    for ($x=0; $x<sizeof($rooms_array); $x++)
     {           
     echo "<option value='$rooms_array[$x]'>".$rooms_array[$x]."</option>";
     }
     ?> 
    </select>
    <input  class="submit_btn" value="Submit" type="submit" name="Submit";/>      
  </form>

我试着通过$_GET将room=028设置为选项,但它不起作用,而且单选按钮我不知道从哪里开始。

您可以使用php来实现这一点:

<?php 
$selectedPerion = $_GET['period'];
for($x = 1; $x < 6; $x++):  // short notation to separate php and html better?>
<input type="radio" name="bookperiod" id="bookperiod" value="<?php echo $x ?>" required 
<?php echo ($selectedPerion == $x ? 'checked' : ''); //so called 'ternary operator' ?>/>
<?php echo $x ?>
<br>
<?php endfor; ?>

或者,您可以使用jQuery设置"checked"属性,或者模拟点击正确的按钮,但那是另一回事。

这与select下拉列表类似,您必须将"selected"属性添加到适当的"标记中,或者使用jQuery设置bookroom字段的值。