How to display data from sql into a formidable form dropdown field

By: Judah Ogu | Asked: 10/26/2023
ForumsCategory: How-toHow to display data from sql into a formidable form dropdown field
Judah Ogu asked 9 months ago

Hi there, I am trying to display data from my SQL as a drop-down field on a formidable form by way of hooking into the Formidable Forms filter. My code currently connects to the DB and fetches the data, but I'm having issues displaying the data through the drop-down field in the form. Nothing comes up. I previously tried displaying it via shortcode, which worked well on the HTML field in the formidable form, but apparently any input on the HTML field was not recognized on submission. Any help on this will be very much appreciated. Here is my code: 

 

Start your code here
add_filter('frm_setup_new_fields_vars', 'populate_dropdown_options');
function populate_dropdown_options($values) {
    if ($values['field_key'] == '***') {
        ob_start();

        $db_server = "***";
        $db_username = "***";
        $db_password = "***";
        $db_name = "***";

        $conn = new mysqli($db_server, $db_username, $db_password, $db_name);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $query = "SELECT * FROM ***";
        $result = $conn->query($query);

        if ($result->num_rows > 0) {
            echo '<select name="***">';
            while ($row = $result->fetch_assoc()) {
                echo '<option value="' . $row['serial_numbers'] . '">' . $row['serial_numbers'] . '</option>';
            }
            echo '</select>';
        } else {
            echo "No data available.";
        }

        $conn->close();

        $values['options'] = ob_get_clean();
    }

    return $values;
}

 

1 Answers
Victor Font answered 9 months ago
See this: https://formidable-masterminds.com/populate-dropdown-from-sql-query/

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right