Possible ways of generating birthdate fields in a registration form
I am developing a website w开发者_如何学编程hich requires user registration. The fields in user registration are the first and last name, gender, birthdate,location(country). Of these I am using select field or dropdown list for gender, birthdate and location. For gender its easy to code as there are only two options to be specified. For birthdate however I have day, month, year dropdown lists seperate for each. Also for countries I need a list of 200 countries atleast Then for populating values in these fields using normal programming would be a dumb task. One option I have is using array and for loop for displaying the values. But the array would be too long and it would look unsmart in the code. I am using codeigniter for developing the website. What are the possible ways using codeigniter through which I can achieve this task with minimal and clean code?
You can accomplish all of this client side with javascript.
Use a datepicker control for the birthdate fields. Go for something like jQuery UI Datepicker or even simply <input type="date">
.
For the country list I would store it as an array as your said and I would hook it up with an autocomplete plugin. jQuery to the rescue again with jQuery UI Autocomplete.
For the Date field, I would suggest jQuery DatePicker or even a simple <input type="date" />
will run on modern browsers but will be browser specific.
For the country list, I would suggest you get the official country list. search for ISO 3166
. Here is a link to speed things up for you. You can use the fields you like in a database or simple global array in PHP and then use a dropdown for the list.
Good Luck!
As Radu said, use jquery UI to accomplish the task for collecting the date of birth .
sample code :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" type="text/css" media="all" />
<script>
$(function() {
$( "#dob" ).datepicker({
changeMonth: true,
yearRange: '1905:2011',
dateFormat: "yy/mm/dd",
showAnim: 'drop',
changeYear: true
});
});
</script>
<td><?php echo form_label('Birthday', $dob['id']); ?></td>
<td><?php echo form_input($dob); ?></td>
<td style="color: red;"><?php echo form_error($dob['name']); ?><?php echo isset($errors[$dob['name']])?$errors[$dob['name']]:''; ?></td>
</tr>
<tr>
and for Country list, there are many publicly available database dumps , so I will use a databse to store the list of countries ..
Adding some coolness :
You can detect the user's country and display that as the default option and also with an option to select other countries from the dropdown .
精彩评论