Support

Account

Home Forums Backend Issues (wp-admin) OnChange Select DropDown

Solving

OnChange Select DropDown

  • Hi,

    I created a field “client” with the wp users.
    In my admin page i’d need to listen to on change on this drop down.
    This is what i tried but no effect…
    Thnaks

    in function.php

    function my_acf_admin_head()
    {
    	?>
     
    
    	<script type="text/javascript">
     
     
    		$('select').on('change', function() {alert( this.value );})
    
    	 
    	</script>
    	<?php
    }
    
    add_action('acf/input/admin_head', 'my_acf_admin_head');
    
    
  • …my second question would be:

    I get the WP users in your Dropdown field by login, how could i retrieve user ID onchange in the list ?
    thanks

  • ok it works with this below, could you please now have a look at my second question please ? thanks

    function my_acf_admin_head()
    {
    	?>
     <script type="text/javascript">
    	jQuery(function($){
    		$('select').on('change', function() {alert( this.value );})
    	});
    </script>
    <?php
    }
    
    add_action('acf/input/admin_head', 'my_acf_admin_head');
  • oh yes !!! magical ! this.value gives me directly the user ID, !!

    when select on change , how could i set my other act fields with new values please ?
    thanks

  • In fact , more generally this is my case :

    I created a custom post type and added the ACF fields in the admin post page.

    My first ACF fiels is a WP User Dropdown, and when loading page or changing value in drop down list i need to update all fiels that depends on $user_id

    thanks

  • The walkthrough to follow.

    On javascript you should
    – get the user ID (there are functions to do that)
    – add it to the ACF ajax query.

    For the other fields depending on user ID value :
    On PHP you should use query filters (there are some for taxs, posts, relations)
    – get the $_POST[ID] (added by JS before)
    – use it.

    When you load the page (ie for an update) , $_POST[ID] does not exist but you can use get_field if you need to use your query filters with user ID

  • RogerBSulli, I think the best is to open first a new thread.

    Regarding your problem, it coulb be easily solved with few JS code.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘OnChange Select DropDown’ is closed to new replies.