Populate from sql query

Home Forums Quform WordPress Populate from sql query

This topic is: not resolved
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36468
    soylatino
    Participant

    Hello,

    First of all, I want to thank everyone for contributing to this fantastic plugin. I need a little help to finish this. I want my jQuery script to listen for changes in selectors 103 and 104, sending an AJAX request to the WordPress server, where the correct options will be retrieved based on the values selected in those selectors. Then, selector 105 will be dynamically updated with the options returned by the server. The SQL query is correct and it shows results in MySQL. The script does not show any errors. However, when I debug the code in the console, nothing changes when I select the menus. Selector 103 and 104 do display the results, but they are not detected in the console debugging.

    Here’s my custom plugin:

     
    /**
     * Función para poblar el formulario con las opciones de tarifas.
     *
     * @param Quform_Form $form El formulario Quform.
     */
    // Llenar el menú de selección quform_1_105 con opciones de tarifa
    add_action('quform_pre_display_1', function (Quform_Form $form) {
        global $wpdb;
    
        // Función para obtener las opciones de tarifa
        function get_tarife_options($laufzeit_id, $preisklasse_id, $include_price = false) {
            global $wpdb;
    
            $select_clause = $include_price ? 'CONCAT(t.tarife_name, " : ", t.preis) AS tarife_name_price' : 't.tarife_name';
    
            $query = $wpdb->prepare('
                SELECT t.tarife_id, ' . $select_clause . '
                FROM my_tarife t
                INNER JOIN my_tarife_laufzeit tl ON t.tarife_id = tl.tarife_id
                INNER JOIN my_tarife_preisklasse tp ON t.tarife_id = tp.tarife_id
                WHERE tl.laufzeit_id = %d
                AND tp.preisklasse_id = %d
            ', $laufzeit_id, $preisklasse_id);
    
            return $wpdb->get_results($query, ARRAY_A);
        }
    
        // Obtener las opciones de my_laufzeit y my_preisklasse
        $laufzeit_options = $wpdb->get_results('SELECT laufzeit_id, laufzeit_desc FROM my_laufzeit', ARRAY_A);
        $preisklasse_options = $wpdb->get_results('SELECT preisklasse_id, preisklasse_desc FROM my_preisklasse', ARRAY_A);
    
        // Llenar los menús de selección quform_1_103 y quform_1_104
        foreach ($laufzeit_options as $option) {
            $form->getElement('quform_1_103')->addOption([
                'value' => $option['laufzeit_id'],
                'label' => $option['laufzeit_desc'],
            ]);
        }
        foreach ($preisklasse_options as $option) {
            $form->getElement('quform_1_104')->addOption([
                'value' => $option['preisklasse_id'],
                'label' => $option['preisklasse_desc'],
            ]);
        }
    
        // Verificar si se han seleccionado valores válidos en los menús quform_1_103 y quform_1_104
        $selected_laufzeit = $form->getValue('quform_1_103');
        $selected_preisklasse = $form->getValue('quform_1_104');
    
        // Depurar los valores seleccionados
        echo "<script>console.log('Selected laufzeit_id: " . $selected_laufzeit . "')</script>";
        echo "<script>console.log('Selected preisklasse_id: " . $selected_preisklasse . "')</script>";
    
        if ($selected_laufzeit !== null && $selected_preisklasse !== null) {
            // Obtener las opciones para el menú de selección quform_1_105
            $intersection_options = get_tarife_options($selected_laufzeit, $selected_preisklasse, true);
    
            // Depurar opciones de tarifa
            echo "<script>console.log(" . json_encode($intersection_options) . ")</script>";
    
            // Llenar el menú de selección quform_1_105
            foreach ($intersection_options as $option) {
                $form->getElement('quform_1_105')->addOption([
                    'value' => $option['tarife_id'],
                    'label' => $option['tarife_name_price'], // Concatenar nombre de la tarifa y precio
                ]);
            }
        } else {
            echo "<script>console.log('No valid selection for laufzeit_id or preisklasse_id')</script>";
        }
    });
    
    

     
    Mi jQuery:

     
    jQuery(function ($) {
        var $select1 = $('.quform-field-1_103 select'),
            $select2 = $('.quform-field-1_104 select'),
            $select3 = $('.quform-field-1_105 select');
    
        $select1.add($select2).on('change', function () {
            var value1 = $select1.val(),
                value2 = $select2.val();
    
            // Realizar una llamada AJAX para obtener las opciones basadas en los valores seleccionados en $select1 y $select2
            $.ajax({
                url: ajaxurl, // Este es el punto final de la URL AJAX de WordPress
                type: 'POST',
                data: {
                    action: 'get_tarife_options', // Esta será la acción AJAX de WordPress
                    laufzeit_id: value1,
                    preisklasse_id: value2
                },
                success: function (response) {
                    var options = [new Option('Please select', '')];
    
                    if (response) {
                        response.forEach(function (option) {
                            options.push(new Option(option.label, option.value));
                        });
                    }
    
                    $select3.html(options).prop('selectedIndex', 0);
                },
                error: function (xhr, status, error) {
                    console.error(status + ': ' + error);
                }
            });
        }).change();
    });
    
    

     
    Thank you for your support!

    • This topic was modified 8 months ago by soylatino.
    #36477
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy