我有一个注册表格,我想在该表格上获得用户计划已从其他页面选择的帐户计划,并将自动填写我的表格以用于帐户类型。
定价网站上的href
signup.php?type=free
signup.php?type=pro
signup.php?type=plus
注册网站中表格的代码
<form class="form-signin" action="pricing.php" method="get">
<div class="form-group">
<label for="type">Account Type</label>
<select class="form-control" id="type">
<option value="free">Free</option>
<option value="pro">Pro</option>
<option value="plus">Plus</option>
</select>
</div></form>
答案 0 :(得分:1)
这将获得$ _GET ['type']变量并为下拉菜单设置selected属性!您也可以使用jQuery(见下文)
<?php
if(isset($_GET['type'])){
$type = $_GET['type'];
}
?>
<form class="form-signin" action="pricing.php" method="get">
<div class="form-group">
<label for="type">Account Type</label>
<select class="form-control" id="type">
<option <?php if($type == 'free'){ echo "selected='selected'"; } ?> value="free">Free</option>
<option <?php if($type == 'pro'){ echo "selected='selected'"; } ?> value="pro">Pro</option>
<option <?php if($type == 'plus'){ echo "selected='selected'"; } ?> value="plus">Plus</option>
</select>
</div></form>
jQuery ...
<?php if(isset($_GET['type'])){ ?>
<script>
$(document).ready(function(){
var type = "<?php echo $_GET['type'] ?>";
$("#type > option").each(function(){
if($(this).val() == type){
$(this).attr("selected", "selected")
}
})
})
</script>
<?php } ?>