How to pass a field of a model from ajax to a view

I have a belongstomany association in two tables (Users and Groups, intermediate table UsersGroups) and I need to allow a multiple select or a single select on add view depending on profile_id.
This is the code for add action in UsersGroups controller:

    public function add()
    {
        $usersGroup = $this->UsersGroups->newEmptyEntity();
        if ($this->request->is('ajax')) {
			$user_id = $this->request->getData('user_id');
			$user = $this->UsersGroups->Users->get($user_id);
			$campo = ($user->profile_id==3) ? 'groups._ids' : 'group_id';
			$this->set('campo', $campo);
		}elseif ($this->request->is('post')) {
            $usersGroup = $this->UsersGroups->patchEntity($usersGroup, $this->request->getData());
            if ($this->UsersGroups->save($usersGroup)) {
                $this->Flash->success(__('The users group has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The users group could not be saved. Please, try again.'));
        }
        $users = $this->UsersGroups->Users->find('list', ['limit' => 200])->all();
        $nets = $this->UsersGroups->Nets->find('list', ['limit' => 200])->all();
        $groups = $this->UsersGroups->Groups->find('list', ['limit' => 200])->all();
        $this->set(compact('usersGroup', 'users', 'nets', 'groups'));
    }

And this is the code for add view:

<?php
/**
 * @var \App\View\AppView $this
 * @var \App\Model\Entity\UsersGroup $usersGroup
 * @var \Cake\Collection\CollectionInterface|string[] $users
 * @var \Cake\Collection\CollectionInterface|string[] $nets
 * @var \Cake\Collection\CollectionInterface|string[] $groups
 */
?>
<div class="row">
    <aside class="column">
        <div class="side-nav">
            <h4 class="heading"><?= __('Actions') ?></h4>
            <?= $this->Html->link(__('List Users Groups'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
        </div>
    </aside>
    <div class="column-responsive column-80">
        <div class="usersGroups form content">
            <?= $this->Form->create($usersGroup) ?>
            <fieldset>
                <legend><?= __('Add Users Group') ?></legend>
                <?php
                    echo $this->Form->control('user_id', ['options' => $users, 'id' => 'userId']);
                    if(!empty($campo)){
						echo $this->Form->control($campo, ['options' => $groups, 'id' => 'groupsId']);
					}
                    echo '<div id="selectNet" style="display:none;">'.$this->Form->control('net_id', ['options' => $nets]).'</div>';
                    echo '<div id="selectGate" style="display:none;">'.$this->Form->control('main_gate', ['options' => ['A' => 'A', 'B' => 'B']]).'</div>';
                ?>
            </fieldset>
            <?= $this->Form->button(__('Submit')) ?>
            <?= $this->Form->end() ?>
        </div>
    </div>
</div>

<script>
$(function(){
	$('#userId').change(function(){
		$.ajax({
			method:"POST", 
			url:"<?= $this->Url->build(['controller' => 'UsersGroups', 'action' => 'add']) ?>",
			data:{
				user_id:$(this).val()
			},
			success: function(data) {
				<?php
					if(!empty($profile_id)) {
						switch ($profile_id) {
							case "4": echo "$('#selectGroup').show();$('#selectNet').show();$('#selectGate').hide();";
								break;
							case "5": echo "$('#selectGroup').show();$('#selectNet').show();$('#selectGate').show();";
								break;
							case "6": echo "$('#selectGroup').show();$('#selectNet').show();$('#selectGate').show();";
								break;
							case "7": echo "$('#selectGroup').show();$('#selectNet').show();$('#selectGate').show();";
								break;
						}
					}
				?>
			},
			headers:{
				'X-CSRF-Token':$('meta[name="csrfToken"]').attr('content')
			}
		})
	})
})
</script>

How can I solve this issue?

Please answer this question, I need yo solve it as soon as possible

Maybe tell us a little bit about what part of this is working and what is not?

can you just explain little more what you exactly want to do? Is your data saved into table or not?

Actually belognsToMany relationship working like this way if you want to save it… [‘user_group’=>['_ids]=>[1,2,3,4]] so make sure your entity structure like this way so then it automatically save records into Junction table UsersGroups .

What is working is the save, it saves the user and the joinData, what is not working is the $this->set(‘campo’, $campo), but what I want to do is that when profile_id of users table is 3 is to allow the user to select multiple options and when is bigger than 3 only allow to select one option, how can I do that?