BuddyDev

Search

[Resolved] Passing field data with jQuery (or alternative)

  • Participant
    Level: Initiated
    Posts: 4
    Andrew on #3405

    I am having trouble passing data from the registration page to auto-fill a field in a users profile with jQuery.

    I have achieved this already for a username with a piece of code I managed to find:

    var url = document.location.href;
    jQuery(document).ready(function() {
    if (url.indexOf(“register/”) >= 0) {
    jQuery(‘label[for=signup_username],#signup_username’).css(‘display’,’none’);
    jQuery(‘#field_1’).blur(function(){
    jQuery(“#signup_username”).val(jQuery(“#field_1”).val());
    });
    }
    });

    I understand that this passes the username value entered at registration in to the primary name field, and also hides the username field.

    I’m now trying to achieve this with a pair of dropdown lists but I can’t seem to get the above to work. I’ve tweaked it several ways but not been successful. My last attempt was:

    var url = document.location.href;
    jQuery(document).ready(function() {
    if (url.indexOf(“register/”) >= 0) {
    jQuery(‘label[for=field_960],#field_960’).css(‘display’,’none’);
    jQuery(‘#field_1140’).blur(function(){
    jQuery(“#field_960 option:selected”).index(jQuery(“#field_1140”).index());
    });
    }
    });

    I used the base script but changed the field names out for the dropdown lists. I’m trying to take the data from field_960 on my registration page and auto-fill field_1140.

    I have tried changing out .val for .index and .text as I’m aware that the val’s for the dropdown lists have different spellings. I was hoping that choosing the selected index would work but no joy. Any help here please?

    I need to do this as my registration page field contains custom user roles based on gender which I need to translate to a usual profile field in order to be able to search by gender in a search form. The current search form in GEO my WP won’t display a search field based on the user role dropdown list.

    Alternatively, if there is a better way to do this other than using jQuery I’d love to know.

    Thanks,
    Andrew

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #3411

    Hi Andrew,
    Welcome to BuddyDev.

    Thank you for posting the question.

    Though the javascript approach is fine, and I have also posted similar snippets earlier on our forum, Let us see another easy approach today.

    Instead of doing things in javascript, Let us hide the fields you don’t need using css. I hope, yopu can do that step without my help(using display:none in css)

    Now, Here is a fail-proof method. You will need to put this snippet in your bp-custom.php

    
    
    function buddydev_check_update_signup_fields() {
    
        //you can access the superglobal $_POST and set values in it too
        //for example
        $_POST['field_1140'] = $_POST['field_960'];
    
        //and so on
    
    }
    add_action( 'bp_signup_pre_validate', 'buddydev_check_update_signup_fields' );
    
    

    Please give it a try and let me know if this works or not?

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Andrew on #3517

    Hi Brajesh,

    Unfortunately the above code is not working as intended.

    I’ve added the code to my bp-custom.php in wp-content/plugins but the sign up field data does not get copied to the user data field after registration, field_1140 remains empty in the user profile.

    I have tried switching the fields in the code and this results in the user not being able to sign up as field_960 is a required field and it reverts to empty, field_1140’s value, upon submit.

    The registration field that I’m having the user fill in is a selection of custom user types specified by the BP Member Type plugin. The user field I want this data copied to has the same index values, but the field value may be different as the BP Member Type value slugs are spelled differently (no uppercase, or spaces.) May this be an issue?

    Thanks,
    Andrew

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #3523

    Hi Andrew,
    I believe it is not the above code but the validation that is failing. The member type plugin uses lower case values for the names.

    Try changing this

    
     $_POST['field_1140'] = $_POST['field_960'];
    

    to

    
     $_POST['field_1140'] = strtolower( $_POST['field_960'] );
    

    Most probably that should do it. Otherwise, I will advise to check for the values and see if they are same as the key of the member type.

  • Participant
    Level: Initiated
    Posts: 4
    Andrew on #3539

    Hi Brajesh,

    This still doesn’t solve the issue.

    I have altered the member type keys and the field names to the exact same spellings besides capitalization. I had to remove some slashes and exchanged them for hyphens. I’ve also tried altering the field names to the same spelling with all lower case so that the values are identical when I inspect both drop down boxes. Still no luck.

    Is there a way to identify the index number of the selected value on the registration form and use that to force selection of that index in the user field drop down box?

    Thanks,
    Andrew

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #3542

    Hi Andrew,
    Can you please point me to your registration page. I will be in a much better position to help you if I can see the fields.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Andrew on #3545

    Hi Brajesh,

    The registration page can be found here:

    http://swingbuddy.co.uk/join/

    On the registration page ‘User Type’ is ‘field_960’

    After registration, ‘field_1140’ can be found at Profile > Edit > About.

    Thank you for your continued effort, I really appreciate it.

    Regards,
    Andrew

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #3546

    Hi Andrew,
    Now, I can see the problem.

    Field 1140 is not part of your registration forms, so BuddyPress is not saving it even if we put it in the post fields. Earlier, I assumed that you had hidden this field using css and that’s why i suggested the above suggestion.

    For you, a better option will be using the following code

    
    function buddydev_update_related_field( $user_id ) {
    	
    	
    	//get the field value
    	
    	$field_data = xprofile_get_field_data(960, $user_id );
    	
    	if ( ! empty( $field_data ) ) {
    		
    		xprofile_set_field_data( 1140, $user_id, $field_data );
    	}
    	
    }
    add_action( 'bp_core_activated_user', 'buddydev_update_related_field' );
    
    

    On activation, we fetch and update the field. Please test it and let me know if it works or not?

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Andrew on #3547

    Brajesh,

    This seems to be working great now.

    I had to alter the profile fields back so that the field text was identical rather than the field spellings themselves and then it worked.

    Thank you so much for your support. Great job!

    Thanks again,
    Andrew

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #3550

    Thank you Andrew,
    I am happy it is resolved now.

    Regards
    Brajesh

The topic ‘ [Resolved] Passing field data with jQuery (or alternative)’ is closed to new replies.

This topic is: resolved