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...

3
  • Where are you trying to put the data in WordPress?? Commented Jul 6, 2019 at 13:18
  • I registered an action with add_action( 'wp_ajax_assign_categories', 'assign_categories' ); and writing the assign_categories callback function.
    – user164608
    Commented Jul 6, 2019 at 14:29
  • Just a short addition: you don't have to JSON.stringify and json_decode the documents Array. The $_POST is able to transfer arrays. Also, "echo" is for strings only. Use var_dump or print_r to echo the contents of your array in PHP. Commented May 25, 2023 at 6:58

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.