AJAX callback

AJAX callback

callback_ajaxThis event makes an Ajax request,
to register the request, in the function.php file use the function:
gol_set_callback ( ‘event_name’, ‘your_function’ );
callback_ajax_paramsthis event serves to transfer parameters to the php function.
before_openThis event has two parameters:
1) the first parameter returns the code as a jQuery object
2) transfer data from the php file to the second parameter of the event using this function “gol_callback_parameters( array() )”
after_openThis event has two parameters:
1) the first parameter returns the code as a jQuery object
2) transfer data from the php file to the second parameter of the event using this function “gol_callback_parameters( array() )”

the ‘gol_set_callback‘ function takes two parameters:
1. the first one to register the name ‘callback_ajax‘.
2. the second is the name of the function to run.

<?php 
/* to avoid errors, if the plugin suddenly disconnects, use "function_exists" (recommended) */
if( function_exists('gol_set_callback') ){
    gol_set_callback( 'your_example_callback_name_1', 'your_example_callback_function_1' );
}
function your_example_callback_function_1( $params ){
    if( $params['example_category'] == 'plants' ){
        $return_params = array(
            'vegetables' => array( 'carrots', 'potatoes', 'cucumber' ),
            'fruit' => array( 'cherry', 'apple', 'peach' ),
        );
    } elseif( $params['example_category'] == 'animals' ) {
        $return_params = array(
            'cats' => array( 'Canadian Sphinx', 'Maine Coon', 'Scottish Fold' ),
            'dogs' => array( 'Doberman', 'Pekingese', 'Husky' )
        );
    }
    /* to avoid errors, if the plugin suddenly disconnects, use "function_exists" (recommended) */
    if( function_exists('gol_callback_parameters') ){
        /* returns data to the second parameter of the "before_open" event */
        gol_callback_parameters( $return_params );
    } ?>
    <h3>You have selected a category "<?php echo $params['example_category']; ?>"</h3>
    <label>
        Subcategory
        <select name="lightbox_example_subcategory">
            <option value="">Select a subcategory</option>
            <!-- additional parameters will be added here in JavaScript -->
        </select>
    </label>
    <ul class="subcategory-list"></ul>
    <?php
}

jQuery('.open-example-lightbox').GreyOwlLightbox('click', {
     callback_ajax : 'your_example_callback_name_1',
     callback_ajax_params : function(){
         var send_params = {
             'example_category' : jQuery('[name="example_category"]').val(),
         }
         return send_params ;
     },
     before_open : function( content, params ){
         var returned_value = params;
         var added_options = '';
         jQuery.each( returned_value, function( key, val ){
             console.log(content.find('[name="lightbox_example_subcategory"]'));
             added_options += '<option value="'+key+'">'+key+'</option>';
         });
         content.find('[name="lightbox_example_subcategory"]').append( added_options );

         content.find('[name="lightbox_example_subcategory"]').change(function(){
             var subcat = jQuery( this ).val();
             var list_str = '';
             jQuery.each( params[ subcat ], function( key, val ){
                 list_str += '<li>'+val+'</li>';
             });
             content.find('.subcategory-list').html( list_str );
         });
     },
     after_open : function( content, params ){
         // the code fires after closing the lightbox
     },
 });

example:

Leave a Reply

Your email address will not be published. Required fields are marked *