1

I have a registration form where I want to be able to get the account plan users have selected form another page and will autofill my form for account type.

The a href from the pricing site

signup.php?type=free
signup.php?type=pro
signup.php?type=plus

Code for the form in registration site

<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>
Reon Low Yiyuan
  • 123
  • 2
  • 2
  • 10
  • You haven't provided any information regarding how your HTML is served... is this some kind of static html file? Are you using php to serve it or some other framework? The importance here is that is that you can interpolate the php $_GET into an html "selected" on the option... but if this is static HTML not so much. More details on your implementation would be great. – ThatTechGuy Jan 27 '19 at 02:47
  • I am using PHP for the signup and also the pricing. – Reon Low Yiyuan Jan 27 '19 at 02:51
  • This answer should get you close https://stackoverflow.com/questions/3608359/specify-a-default-selected-item-for-html-form-drop-downs I like the javascript approach and instead of $INDEX variable you could just use $_GET['type'] and make sure the ids match. – ThatTechGuy Jan 27 '19 at 03:06

1 Answers1

1

This will get the $_GET['type'] variable & set selected attribute for the drop down! You could also do it with jQuery (seen below)

<?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 } ?>
Nerdi.org
  • 895
  • 6
  • 13