0

I have a HTML form with 2 select boxes with multiple selection. I get the selected values on form submit with jQuery and store them in 2 different arrays:

$('#assign_categories_form').on('submit', function (event) {
    event.preventDefault();

    var documents = [];
    $.each($("#documents_auth_select option:selected"), function(){
        documents.push($(this).val());
    });
    if (documents.length === 0) {
        $('#modal_message_title').html( 'Nessun documento selezionato...' );
        $('#modal_message_p').html( 'È necessario selezionare almeno un documento a cui assegnare delle categorie!' );
        $('#modal_message_div').modal();
        return false;
    }

    var categories = [];
    $.each($("#dl_categories option:selected"), function(){
        categories.push($(this).val());
    });
    if (categories.length === 0) {
        $('#modal_message_title').html( 'Nessuna categoria selezionata...' );
        $('#modal_message_p').html( 'È necessario selezionare almeno una categoria prima di procedere!' );
        $('#modal_message_div').modal();
        return false;
    }

    console.log(documents);

    $.ajax({
        type: 'POST',
        url: ajax_db.ajaxurl,
        contentType: 'application/json',
        data: JSON.stringify({
            action: 'assign_categories',
            documents: documents,
            categories: categories
        }),
        success: function (data) {
            alert(data);
        }
    });

});

I need to pass documents and categories arrays to PHP with AJAX, in past I used JSON.stingify but I can't understand how to do in WordPress...

EDIT:

I tried writing:

documents = JSON.stringify(documents);
            console.log(documents);

            $.ajax({
                type: 'POST',
                url: ajax_db.ajaxurl,
                data: {
                    action: 'assign_categories',
                    documents: documents
                },
                success: function (data) {
                    console.log(data);
                }
            });` and in the PHP callback `public function assign_categories() {

    $documents = json_decode($_POST["documents"]);

    echo $documents;
    wp_die();

}

with the PHP callback:

public function assign_categories() {

    $documents = json_decode($_POST["documents"]);

    echo $documents;
    wp_die();

}

but the callback return empty...

  • Where are you trying to put the data in WordPress?? – ChristopherJones Jul 6 '19 at 13:18
  • I registered an action with add_action( 'wp_ajax_assign_categories', 'assign_categories' ); and writing the assign_categories callback function. – user164608 Jul 6 '19 at 14:29
0

If assign_categories() is in a class, you have to call the action in a different way.

add_action( 'wp_ajax_assign_categories', 'assign_categories' );

becomes

add_action( 'wp_ajax_assign_categories', array( $this, 'assign_categories' ) );

The $this in this context is the class.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy