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

时间:2014-04-29 20:43:41

标签: php html

我已成功将数据以url:

的形式发送到我的页面
http://localhost:8101/Tutorials/sandbox/myfiles/make_a_booking.php?period=2&date=04/29/2014&room=028

但我正在努力将数据放入表单中。但是,如何将period = 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将房间= 028放入选项,但它没有工作,单选按钮我不知道从哪里开始。

1 个答案:

答案 0 :(得分:1)

你可以使用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设置&#39;已检查&#39;属性或模拟点击正确的按钮,但这是另一个故事。

与选择下拉菜单类似,您必须添加“已选择”#39;财产到适当&#34;&#34;使用jQuery标记或设置bookroom字段的值。