admin管理员组文章数量:1122832
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...
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...
Share Improve this question edited Jul 6, 2019 at 17:03 MikeNGarrett 2,6611 gold badge20 silver badges29 bronze badges asked Jul 6, 2019 at 13:14 user164608user164608 3 |1 Answer
Reset to default 0If 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.
本文标签: How to pass array from jQuery to PHP with AJAX in WordPress
版权声明:本文标题:How to pass array from jQuery to PHP with AJAX in WordPress? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288087a1928020.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
add_action( 'wp_ajax_assign_categories', 'assign_categories' );
and writing theassign_categories
callback function. – user164608 Commented Jul 6, 2019 at 14:29