Skip Navigation

[Resolved] Conditional Message When Changing User Role

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to display conditional message when user select different option from select dropdown box with CRED form

Solution:
You need to chose different approach using jQuery - javascript to display conditional message based on selection of option from dropdown select.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/conditional-message-when-changing-user-role/#post-723144

Relevant Documentation:

This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by KentS9937 6 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#721525

I am trying to display a message to users when they are changing the role of a user using a dropdown I've created. I'm comparing the value of the selection to the post author's role. Nothing happens when I use the code shown below.

<div class="col-sm-2 wtn-label">Position</div>
	<div class="col-sm-4 wtn-field"><div class="form-group">[cred_field field='hr-position' post='hr-profiles' display='select' single_select='true' class='dropdown' output='bootstrap']
	</div>[wpv-conditional if="( [get_author_role] ne $(wpcf-hr-position) )"]
      CAUTION! Did you mean to change the user role?[/wpv-conditional]</div>
</div>

Here's the function which gets the author's role. I believe it is working properly.

function get_author_role()
{
    global $authordata;
  
    $author_roles = $authordata->roles;
    $author_role = array_shift($author_roles);
  
    return $author_role;
}
  
add_shortcode( 'user_role_func', 'get_author_role' );

I am expecting the message "CAUTION! Did you mean to change the author's role?" to display when the selection is changed from the original value.

#723144

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - conditional shortcode will not work real time selection. You need to chose different approach using jQuery - javascript.

Add hidden field to CRED form and assign custom shortcode you created as value. For example:

<input type="hidden" value="[get_author_role]" name="current_author_role" id="current_author_role" />

Use following code - where we wrap the message with div which has assigned ID "display_message":

<div class="col-sm-2 wtn-label">Position</div>
    <div class="col-sm-4 wtn-field"><div class="form-group">[cred_field field='hr-position' post='hr-profiles' display='select' single_select='true' class='dropdown' output='bootstrap']
    </div>
<div id="display_message" style="display:none;'>
      CAUTION! Did you mean to change the user role?
</div>
</div>
</div>

And Try to add following JS code:

jQuery(document).ready(function($){

$('select[name^='wpcf-hr-position').on('change', function() {
 author_role = $("#current_author_role").val();
If(author_role != this.value) {
   $('#display_message').show();
 }else{
$('#display_message').hide();
}

});

});

I tried my best to give solution as per your current setup. Please feel free to adjust the code as needed and if required.

#762036

Thanks for your help