Reputation: 3097
I have a form with select tags for date of birth. The day and month default to the first options (the 1st, and January), which is fine. For the year, I use selected="selected" for 1975, since this is the average age for my users.
What I want to be able to do is to overwrite this default year value by passing the variable in a URL.
For example:
www.domain.com?dob_day=14&dob_month=july&dob_year=1980
The reason being is that I want to send an email and pre-populate the fields they filled out.
So what's happening right now when I look at the source code is both the year 1975 is selected, and the year passed in the variable.
What would be the best way to achieve the above?
Thank you very much.
Upvotes: 0
Views: 114
Reputation: 1993
I think fastest and most clean way to achieve that is using a short javascript to set proper value when the page is loaded.
Assuming you read data like this:
$selectedYear = isset($_GET['dob_year']) ? intval($_GET['dob_year']) : null;
And you have select block like this:
<select id="selectedYear" name="selectedYear" class="combobox">
...
</select>
You can add this to the bottom of page:
<?php if ($selectedYear) { ?>
<script type="text/javascript">
window.onload = function() { document.getElementById('selectedYear').value = <?php echo $selectedYear; ?>; }
</script>
<?php } ?>
Upvotes: 0
Reputation: 985
To overwrite the default values, you would use something like this to retrieve the variables from the URL:
$dob_year = $_GET["dob_year"];
$dob_day = $_GET["dob_day"];
$dob_month = $_GET["doby_month"];
And then for each select drop down, you could do this:
echo "<select id = \"dob_month\">";
for($i=1;$i<=12;$i++)
{
$sel = $i==$dob_month ? "selected" : "";
echo "<option value=\"$i\" $sel>$i</option>";
}
echo "</select>";
Upvotes: 1