我为生日数据定制了个人资料字段。
function my_user_field( $user ) {
$birthday_day = esc_attr( get_the_author_meta( \'birthday_day\', $user->ID ) );
$birthday_month = esc_attr( get_the_author_meta( \'birthday_month\', $user->ID ) );
$birthday_year = esc_attr( get_the_author_meta( \'birthday_year\', $user->ID ) );
?>
<h3><?php _e(\'More About You\'); ?></h3>
<table class="form-table">
<tr>
<th>
<label for="birthday"><?php _e(\'Birthday\'); ?>
</label></th>
<td>
<span class="description"><?php _e(\'When is your birthday?\'); ?></span><br>
<form action="">
<select name="birthday_day" id="birthday_day">
<?php
for ($i=1; $i<=31; $i++)
{
echo \'<option value="\';
echo $i;
echo \'"\';
if ($birthday_day == $i ) {
echo \'selected\';
}
echo\' >\';
echo $i;
echo\'</option>\';
};
?>
</select>
<select name="birthday_month" id="birthday_month" onchange="updateDays();">
<option value="1" <?php if ($birthday_month == \'1\') { ?>selected<?php }?> >January</option>
<option value="2" <?php if ($birthday_month == \'2\' ) { ?>selected<?php }?> >February</option>
<option value="3" <?php if ($birthday_month == \'3\' ) { ?>selected<?php }?> >March</option>
<option value="4" <?php if ($birthday_month == \'4\' ) { ?>selected<?php }?> >April</option>
<option value="5" <?php if ($birthday_month == \'5\' ) { ?>selected<?php }?> >May</option>
<option value="6" <?php if ($birthday_month == \'6\' ) { ?>selected<?php }?> >June</option>
<option value="7" <?php if ($birthday_month == \'7\' ) { ?>selected<?php }?> >July</option>
<option value="8" <?php if ($birthday_month == \'8\' ) { ?>selected<?php }?> >August</option>
<option value="9" <?php if ($birthday_month == \'9\' ) { ?>selected<?php }?> >September</option>
<option value="10" <?php if ($birthday_month == \'10\' ) { ?>selected<?php }?> >October</option>
<option value="11" <?php if ($birthday_month == \'11\' ) { ?>selected<?php }?> >November</option>
<option value="12" <?php if ($birthday_month == \'12\' ) { ?>selected<?php }?>>December</option>
</select>
<select name="birthday_year" id="birthday_year" onchange="updateDays();">
<?php
for ($i=2010; $i>1930; $i--)
{
echo \'<option value="\';
echo $i;
echo \'"\';
if ($birthday_year == $i ) {
echo \'selected\';
}
echo\' >\';
echo $i;
echo\'</option>\';
};
?>
</select>
</form>
</td>
</tr>
</table>
<?php
}
function my_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( \'edit_user\', $user_id ) )
return FALSE;
update_usermeta( $user_id, \'birthday_day\', $_POST[\'birthday_day\'] );
update_usermeta( $user_id, \'birthday_month\', $_POST[\'birthday_month\'] );
update_usermeta( $user_id, \'birthday_year\', $_POST[\'birthday_year\'] );
}
add_action( \'show_user_profile\', \'my_user_field\' );
add_action( \'edit_user_profile\', \'my_user_field\' );
add_action( \'personal_options_update\', \'my_save_custom_user_profile_fields\' );
add_action( \'edit_user_profile_update\', \'my_save_custom_user_profile_fields\' );
它通常保存到db,但有一个失败。我可以选择例如“2月31日”,这很糟糕。如何对这些进行验证?我想如果用户选择“2月31日”并提交表单,则其日期将保存为“2月28日”,但如果选择的年份是闰年,则保存为“2月29日”。
我知道可以使用javascript制作更灵活的表单,但如何使用PHP实现呢?