Reputation: 21
I have had a developer create some code below in my wordpress footer.php file to predefault a set of fields based on the user's profile.
The text fields seem to default fine however the select option (gender) does not default and the user has to select this option, even though the value is stored against their profile.
Is there a way to update this code to have the gender default? I think it is something to do with using .val()??
Here is the code:
<?php
$current_user = wp_get_current_user();
/**
* @example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
* return;
*/
$username = $current_user->user_login;
$email = $current_user->user_email;
$firstname = $current_user->user_firstname;
$lastname = $current_user->user_lastname;
$displayname = $current_user->display_name;
//$user_id = $current_user->phone;
if ( is_user_logged_in() ){
global $current_user;
get_currentuserinfo();
$email = $current_user->user_email;
$name = $current_user->user_firstname.' '.$current_user->user_lastname;
$golflink = $current_user->dbem_golf_link;
$home_club = $current_user->dbem_home_club;
$handicap = $current_user->dbem_handicap;
$gender = $current_user->dbem_gender;
}
?>
jQuery( document ).ready(function() {
jQuery('#attendee_name').val("<?php echo $firstname.' '.$lastname; ?>");
jQuery('#golflink').val("<?php echo $golflink; ?>");
jQuery('#home_club').val("<?php echo $home_club; ?>");
jQuery('#handicap').val("<?php echo $handicap; ?>");
jQuery('#gender').val("<?php echo $gender; ?>");
Any advice would be great.
From other reading, can I just change: jQuery('#gender').val(""); to jQuery('select#gender').val("");
I tried the above but no changes.
The select I created in wordpress was 'Male' and 'Female'
Thanks
Upvotes: 1
Views: 128
Reputation: 11987
To give value for a select
in jquery, the value you are passing should be same as the value of the select box.
Example 1:
$gender = "male" or "female";//any value you want
<select name="gender" id="gender">
<option value="male">Male</option>
^ this should be same as your $gender
<option value="female">Female</option>
^ this should be same as your $gender
</select>
Then
jQuery('#gender').val("<?php echo $gender; ?>");
$gender
has value as male
, so male will be selected.
Example 2:
$gender = "male" or "female";//any value you want
<select name="gender" id="gender">
<option value="0">Male</option>
^ this should be same as your $gender
<option value="1">Female</option>
^ this should be same as your $gender
</select>
Then
jQuery('#gender').val("<?php echo $gender; ?>");
$gender
has value as 0 or 1
, But select box value is male
and female
. So even though you have passed the value, it will not work.
Upvotes: 2