Support

Account

Home Forums Backend Issues (wp-admin) How to create dinamically selectboxes from database

Unread

How to create dinamically selectboxes from database

  • I would like to know, how to create dinamically selectboxes populated from database, depending of the current value selected from other selectbox.

    The options values are going to be pulled them from database with a query.

    This is the function, that i’m using to generate the values for the first select box.

    function select_states(){
    
    $states_table = $wpdb->prefix.rep_loc::States_TB;
    
        $query = "Select abbreviation, name from $states_table where in_map = 1";
    
        // get the textarea value from options page without any formatting
    
        $choices = $wpdb->get_results($query);
    
        // reset choices
        $field['choices'] = array();
     
        // loop through array and add to field 'choices'
        if( is_array($choices) )
        {
            foreach( $choices as $choice )
    
            {
                $field['choices'][ $choice->abbreviation ] = $choice->name;
            }
        }
     
        // Important: return the field
        return $field;
    
    }

    using this, and the filter for load the field, is working like a charm to me right now.

    But now, i need, if someone selects an option from this selectbox, generate a second one, with the options depending of the selected, in same way, getting the info from the database.

    How can i send, as variable the option selected by JSON or something to generate the second selectbox?

Viewing 1 post (of 1 total)

The topic ‘How to create dinamically selectboxes from database’ is closed to new replies.