Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Edited for clarity and formatting.
Source Link
MikeNGarrett
  • 2.7k
  • 1
  • 20
  • 29
$('#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:

EDIT: I tried writing:

$('#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:

$('#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:

added 865 characters in body
Source Link
user164608
user164608

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

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

Source Link
user164608
user164608

How to pass array from jQuery to PHP with AJAX in WordPress?

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