wordpress - How to get a dynamic dropdown with an ACF Relationship field -


my website lists courses , course providers. i'm using acf relationship assign providers each course. both courses , course providers custom post types have contact form each single course page , need send enquiry selected course provider dropdown. i'm trying have dynamic field where, once provider has been selected, fetch email (assigned acf field) form field , submit form specific email. i'm getting both list of assigned providers , emails in following code.

               <select name="supplier" id="supplier" class="form-control">                     <option value="">---</option>                     <?php                     $posts = get_field('course_providers');                     if( $posts ): ?>                         <?php foreach( $posts $post): ?>                             <?php setup_postdata($post); ?>                             <option value="<?php the_title(); ?>"><?php the_title(); ?></option>                         <?php endforeach; ?>                         <?php wp_reset_postdata(); ?>                     <?php endif; ?>                 </select>                  <select name="supplier_email" id="supplier_email" class="form-control">                     <option value="">---</option>                     <?php                     $posts = get_field('course_providers');                     if( $posts ): ?>                         <?php foreach( $posts $post): ?>                             <?php setup_postdata($post); ?>                             <option value="<?php the_field('email_address'); ?>"><?php the_field('email_address'); ?></option>                         <?php endforeach; ?>                         <?php wp_reset_postdata(); ?>                     <?php endif; ?>                 </select> 

i hope can done jquery.

thanks!

to make things easier add email address data attribute tag each provider title so:

<select name="supplier" id="supplier" class="form-control">                     <option value="">---</option>                     <?php                     $posts = get_field('course_providers');                     if( $posts ): ?>                         <?php foreach( $posts $post): ?>                             <?php setup_postdata($post); ?>                             <option value="<?php the_title(); ?>" data-email="<?php the_field('email_address'); ?>"><?php the_title(); ?></option>                         <?php endforeach; ?>                         <?php wp_reset_postdata(); ?>                     <?php endif; ?>                 </select> 

then in jquery can like:

$(document).ready(function() {     $('select#supplier').change(function() {         var emailaddress = $('select#supplier').find(':selected').data('email');     });     // var emailaddress on form submit here }); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -