Title: [Plugin: Contact Form 7]Create a selectbox from database?
Last modified: August 20, 2016

---

# [Plugin: Contact Form 7]Create a selectbox from database?

 *  [wzshop](https://wordpress.org/support/users/wzshop/)
 * (@wzshop)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/plugin-contact-form-7create-a-selectbox-from-database/)
 * Hi guys,
    I am trying to create a custom dropdown (select) box in my Contactform
   with values (post titles from category id 5) from the database, called by a custom
   shortcode.
 * To do so i put the following function/code in my 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); 
   
       	$output .= "<option> the_title(); </option>";
   
       	endforeach;
       $output .= "</select>";
       return $output;	
   
       }
       ```
   
 * When i add the shortcode [postdropdown] in my contactform a dropdown menu does
   show up, but the value is “the_title();” instead of the concerning post title
   from category_id 5.
 * What am i doing wrong? Can anyone help me?
    thanks

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [wzshop](https://wordpress.org/support/users/wzshop/)
 * (@wzshop)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/plugin-contact-form-7create-a-selectbox-from-database/#post-2808210)
 * Got it fixed already. Sometimes I’m totally blind:)
 * The code to make this work is:
 *     ```
       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]
 * Cheers!
 *  Thread Starter [wzshop](https://wordpress.org/support/users/wzshop/)
 * (@wzshop)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/plugin-contact-form-7create-a-selectbox-from-database/#post-2808215)
 * 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.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘[Plugin: Contact Form 7]Create a selectbox from database?’ is closed 
to new replies.

## Tags

 * [custom shortcode](https://wordpress.org/support/topic-tag/custom-shortcode/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 2 replies
 * 1 participant
 * Last reply from: [wzshop](https://wordpress.org/support/users/wzshop/)
 * Last activity: [13 years, 12 months ago](https://wordpress.org/support/topic/plugin-contact-form-7create-a-selectbox-from-database/#post-2808215)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
