大家好需要帮助Ajax发送对象数组数据到php我的观点是从javascript获取2个数组数据然后使用Ajax将数据对象发送到php并保存到数据库这个我的信息
var group_data = [];
group_data.push({
member: member_ids,
overlap: overlaps
});
[Object]
0:Object
member: Array[3]
0:"1070"
1:"1179"
2:"1180"
overlap: Array[4]
0:"Friday 02:30 UTC - Friday 03:30 UTC"
1:"Friday 10:00 UTC - Friday 23:00 UTC"
2:"Sunday 03:00 UTC - Sunday 04:00 UTC"
3:"Sunday 08:00 UTC - Sunday 09:00 UTC"
这是我的Ajax发送数据
$.post(ajaxurl,{ 'action': 'send_matching_group', 'group_data': group_data, }, function(data){
}).done(function(){
console.log(group_data);
});
这个我的php从Ajax获取发布数据
add_action( 'wp_ajax_send_matching_group', 'send_matching_group' );
function send_matching_group() {
global $wpdb;
$tableGroup = 'wp_wlm_member_groups';
$tableUser = 'wp_wlm_user_options';
$overlaps = array();
$member_ids = array();
foreach($data as $group_data){
array_push($overlaps,$group_data['overlap']);
array_push($member_ids,$group_data['member']);
}
if ( $wpdb->insert( $tableGroup, array('name' => 'Matching Group','on_status' => 'matching_group','on_step'=>'matching_group' ,'group_lv' => 'Participants','facilitators' => '', 'start'=>'', 'overlaps'=>$overlaps) )){
$group_id = $wpdb->insert_id;
foreach ( $member_ids as $user_id ){
$data = array('user_id'=>$user_id, 'option_name'=>'group_id', 'option_value'=>$group_id, 'autoload'=>'yes');
$wpdb->replace($tableUser, $data );
}
wp_redirect( 'admin.php?page=group-match-list' );
exit;
}
echo 1;
wp_die();
}
结果是数据库中没有任何内容
答案 0 :(得分:0)
php(server)
方时将array/object
转换为json string
:
使用'group_data': JSON.stringify(group_data)
并在php端执行:
$group_data=json_decode($_POST['group_data'],true);//this should give you the array