6

What's the best method to output "selected" for my form select based on the URL parameter?

In my URL I might have this parameter &term=retail.

How could I tell the below code to select the retail option?

<select class="form-control" name="term" id="saleTerm">
        <option value="all-residential" selected>All Residential</option>
        <option value="apartment">&nbsp;&nbsp;&nbsp;Apartment</option>
        <option value="villa">&nbsp;&nbsp;&nbsp;Villa</option>
        <option value="all-commercial">All Commercial</option>
        <option value="office">&nbsp;&nbsp;&nbsp;Office</option>
        <option value="retail">&nbsp;&nbsp;&nbsp;Retail</option>
</select>

3 Answers 3

9

Easiest approach using Javascript:

var val = location.href.match(/[?&]term=(.*?)[$&]/)[1];   // get params from URL
$('#saleTerm').val(val);   //  assign URL param to select field

PHP way refer to answer by Danijel.

Sign up to request clarification or add additional context in comments.

4 Comments

And to add to my question, how about input fields? ULR &acf_Area=Downtown Burj Dubai <input type="text" name="acf_Area" id="acf_AreaSale">
are you asking about how to assign url params named (acf_Area) to input fields ?
Yes, input text field
var inpVal = location.href.match(/[?&]acf_Area=(.*?)[$&]/)[1]; $('#acf_AreaSale').val(inpVal);
9

The PHP way:

<?php $term = !empty( $_GET['term'] ) ? $_GET['term'] : ''; ?>

<select class="form-control" name="term" id="saleTerm">
        <option value="all-residential" <?php echo $term == 'all-residential' ? 'selected' : ''; ?>>All Residential</option>
        <option value="apartment" <?php echo $term == 'apartment' ? 'selected' : ''; ?>>&nbsp;&nbsp;&nbsp;Apartment</option>
        <option value="villa" <?php echo $term == 'villa' ? 'selected' : ''; ?>>&nbsp;&nbsp;&nbsp;Villa</option>
        <option value="all-commercial" <?php echo $term == 'all-commercial' ? 'selected' : ''; ?>>All Commercial</option>
        <option value="office" <?php echo $term == 'office' ? 'selected' : ''; ?>>&nbsp;&nbsp;&nbsp;Office</option>
        <option value="retail" <?php echo $term == 'retail' ? 'selected' : ''; ?>>&nbsp;&nbsp;&nbsp;Retail</option>
</select>

2 Comments

Ya using server side script for value passed to server, seems more legit
True but maybe for some reason, OP cannot/doesn't want to modify any server side script, but IMHO, should be done like this
4

you can do using jquery this way:

var term= GetURLParameter('term');
$('#saleTerm').val(term);

Here is generic function GetURLParameter():

function GetURLParameter(sParam)
    {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++)
        {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam)
            {
                return sParameterName[1];
            }
        }
    }​

See more Details HERE

1 Comment

looks better than accepted answer, handling more cases

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.