JS DATA POST update taxonomy

Question

How can I call the value of each term with my js function? I need use a Array not just useractivities = $('#user_activities').val();? How?

Here is my code:

HTML VIEW:

<div class="multiselect">

<div id="user_activities_form">
       <label for="user_activities-randonnee">
        <input type="checkbox" name="user_activities[]" id="user_activities-randonnee" value="10028">
        RANDONNEE          </label><br>
       <label for="user_activities-vtt">
        <input type="checkbox" name="user_activities[]" id="user_activities-vtt" value="10029">
        VTT          </label><br>
          
</div>

JS CALL

useractivities   =  $('#user_activities').val(); //HERE array[] ?
         $.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {
                'action'            :   'wpestate_ajax_update_profile',
                'user_activities'         :   useractivities   
            },
            success: function (data) {

                $('#profile_message').empty().append('<div class="login-alert">' + data + '<div>');
            },
            error: function (errorThrown) {
            }
        });

UPDATE FUNCTION (wpestate_ajax_update_profile):

             $current_user   =   wp_get_current_user();
             $userID         =   $current_user->ID;
             $useractivities = get_terms($_POST['user_activities'], array( 'hide_empty' => false ) );

             
 foreach($useractivities as $useractivitie){
    $useractivitieID = $useractivitie->term_id;
    if ('POST' === $_SERVER['REQUEST_METHOD']) {
                if(!isset($_POST['user_activities']))
                wp_set_object_terms($userID,$useractivitieID,'user_activities',false); 
                else 
                wp_set_object_terms($userID,$useractivitieID,'user_activities',true); 

    }   
 }
0
Anarko 3 months 0 Answers 13 views 0

Leave an answer