简体   繁体   English

如何将对象数组数据从Ajax发布到php

[英]How to get object array data from Ajax post to php

Hi guys need help help with Ajax send object array data to php my point is get 2 array data from javascript then use Ajax to send data object to php and save to database this my info 嗨伙计们需要帮助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"

and this my Ajax send data 这是我的Ajax发送数据

$.post(ajaxurl,{ 'action': 'send_matching_group', 'group_data': group_data,  }, function(data){

}).done(function(){
 console.log(group_data);

});

this my php get post data from Ajax 这个我的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();

    }

the result is get nothing in the database 结果是数据库中什么都没有

while sending to php(server) side convert the array/object to json string : 发送到php(server)端将array/object转换为json string

use 'group_data': JSON.stringify(group_data) 使用'group_data': JSON.stringify(group_data)

and on php side do: 并在PHP方面做:

$group_data=json_decode($_POST['group_data'],true);//this should give you the array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM