WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form 7
[resolved] Populate Select Box from database (14 posts)

  1. Katch22
    Member
    Posted 2 years ago #

    Hi,

    Im wondering if its possibly to populate a drop down selection box from content stored within the database (in my case being a list of courses)?

    I have the php script to show the list in regular browser, i've put together an attempt and creating a function to display the code via shortcode without a lot of success.

    I have even looked at some extensions for Contact Form 7 but found nothing that could do this for a select box.

    Any help/advice would be appreciated, I desperate to get this part completed!

    Thanks
    Katch

    http://wordpress.org/extend/plugins/contact-form-7/

  2. Katch22
    Member
    Posted 2 years ago #

    Any ideas?

  3. westleymon
    Member
    Posted 2 years ago #

    I found a solution posted here.

    They populated the dropdown with post titles from a specified category.

    The file you will need to modify is select.php and it is under the "modules" folder with in cf7.
    Open up select.php and on line 93 (right under $label = $value;)
    paste the following code.

    if ($name == 'events') {
    
     $cf7_dropdown = new WP_Query("cat=1");
     while($cf7_dropdown->have_posts()) : $cf7_dropdown->the_post();  
    
    	  if ( !in_array( get_the_title(), $do_not_duplicate ) ) {
    		 $do_not_duplicate[] = array_unique($do_not_duplicate);
    	 	 $html .= '<option value="' . esc_attr(get_the_title()) . '"' . '>' . esc_html(get_the_title()) . '</option>';
    		  array_push($do_not_duplicate, get_the_title());
    
    		}
    
    				endwhile;
        } else {

    In my case, I had a custom post type (Events), that needed to get a list of all events from this day forward (which is why the query string has the meta_value and meta_compare within it).

    Here is the code I used:

    // custom for events list
    if ($name == 'events') {
     $cf7_dropdown = new WP_Query('post_type=event&numberposts=-1&order=ASC&meta_key=event_date&meta_compare=>=&meta_value=' . date("m/d/Y") . '&orderby=meta_value_num');
     while($cf7_dropdown->have_posts()) : $cf7_dropdown->the_post();
     setup_postdata($post);
      $custom = get_post_custom($post->ID);
    	 	  $html .= '<option value="' . esc_attr(get_the_title()) . ' - ' . $custom["event_date"][0] . '"' . '>' . esc_html(get_the_title()) . ' - ' . $custom["event_date"][0] . '</option>';
    endwhile;
        } else { //end custom for events list

    Do note that with this, you'll need to add a another closing bracket to the very end of the file - changing
    <?php } ?>
    to

    <?php }
    } ?>

    Let me know if you've got any questions...

  4. The Todd
    Member
    Posted 2 years ago #

    I can offer a better solution, or at least one that does not require hacking the contact form 7 plugin..

    I'm not sure if this is even the best way, but it works for me!

    I added the plugin:

    http://wordpress.org/extend/plugins/contact-form-7-dynamic-text-extension/

    and then in the form where I want the select box to be, I add some shortcode..

    [autoselectfleet][dynamichidden fleet id:fleet ]

    The page is using a template,

    `$content = do_shortcode( $post->post_content );
    $new_content = str_replace('[autoselectfleet]', $fleet_selector, $content);`

    where $fleet_selector is the selector I made..

    On that select box,

    onchange='document.getElementById('fleet').value=this.value;'

    and I also added a javascript snippet to first populate that box on page load with a preselected value for the dropdown, as they arrive on my form after first picking something somewhere else, and that value is one of the dropdown values, I added select='selected' to the value in the dropdown as well..

    all this simply allowed the email to grab those dynamic hidden values and use them as needed..

    Hope to have helped, lemme know if you need better explanation..

    The Todd

  5. davejuk
    Member
    Posted 2 years ago #

    Hi

    I like your solution Todd but I'm not sure I completely understand the logic of how it's implemented.

    Should I be building the select box HTML in the page template and then replacing the shortcode with it?

  6. davejuk
    Member
    Posted 2 years ago #

    In the end I used a slightly different solution based on the Contact Form 7 Dynamic Text Extension.

    I didn't use any JavaScript and I think this is a less hacky way to do it. From my themes functions.php:

    [Code moderated as per the Forum Rules. Please use the pastebin]

  7. antoine33
    Member
    Posted 2 years ago #

    thank you todd, i try to implement your method.
    i get this php error: "Cannot redeclare createbox()"
    My code is: ( i use a plugin "Shortcode Exec PHP" )

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

  8. antoine33
    Member
    Posted 2 years ago #

    thank you todd, i try to implement your method.
    i get this php error: "Cannot redeclare createbox()"
    My code is: ( i use a plugin "Shortcode Exec PHP" )

    http://pastebin.com/1v1EpPvW

  9. wzshop
    Member
    Posted 2 years ago #

    Anyone who can help us further on this subject? I am also getting an error Todd, what exact code you used?

    Thanks.

  10. wzshop
    Member
    Posted 2 years ago #

    Hi guys,

    This is the code that makes it all work. Maybe not the best script? but it does really work. Simple add this function in functions.php:

    wpcf7_add_shortcode('postdropdown', 'createbox');
    function createbox(){ 
    
    global $post;
    $args = array( 'category' => 5 );
    $myposts = get_posts( $args );
    $output = '<select name="lstdate" id="fleet" onchange="document.getElementById(\'fleet\').value=this.value;"><option></option>';
    foreach ( $myposts as $post ) : setup_postdata($post);
    	$title = get_the_title();
    	$output .= "<option> $title </option>";
    
    	endforeach;
    $output .= "</select>";
    return $output;	
    
    }

    With the shortcode in contactform 7 [postdropdown]. This will show all the posts titles in category 5.

    Cheers!

  11. wzshop
    Member
    Posted 2 years ago #

    To fully get it functioning, use this updated code:

    Updated code:

    wpcf7_add_shortcode('postdropdown', 'createbox', true);
    function createbox(){
    global $post;
    $args = array('numberposts' => 0, 'category' => 5 );
    $myposts = get_posts( $args );
    $output = "<select name='cursus' id='cursus' onchange='document.getElementById(\"cursus\").value=this.value;'><option></option>";
    foreach ( $myposts as $post ) : setup_postdata($post);
    	$title = get_the_title();
    	$output .= "<option value='$title'> $title </option>";
    
    	endforeach;
    $output .= "</select>";
    return $output;
    }

    With the shortcode [postdropdown cursus] in the contactform to show posts from category 5. Put shortcode [cursus] in the mailtemplate.

    cheers.

  12. EnosRussell
    Member
    Posted 2 years ago #

    Hi - how about populating the registration fields from an existing contact data base. The user has already logged in with name + email before they get to the registration screen.

    Has any one already done this?

    Thanks in avance!

    ELR

  13. willwillok
    Member
    Posted 2 years ago #

    Hey WZShop. Thanks this works great...

    Do you know how to adjust this code to get multiple selectors from different catergories?

    thanks!

  14. matthieuscarset
    Member
    Posted 1 year ago #

    Do you have any ideas how to get this work with custom post types instead of category ?

    I've tried this (and that...) but nothing shows up in the drop-down list:

    $args = array( 'numberposts' => 0, 'post_type' => 'offre');

    Thx

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic