dongpinyao2203 2011-07-27 02:29
浏览 46
已采纳

将多个javascript数组传递给PHP页面

I was trying to find an answer for this question for a while but no luck.

The problem is this. On one PHP page i have multiple javascript arrays (lets say 2). One of those arrays is an array that represents pieces of a shirt and the other array contains color of each piece.

  $pieces[0] = 'base';
  $pieces[1] = 'sleeves';
  $pieces[2] = 'pocket';

  $colors[0] = 'white';
  $colors[1] = 'red';
  $colors[2] = 'black';

On top of all that i have a form with where user can enter some data.

Now my question is, how can I pass those two arrays and form data to a PHP page?

Thanks for all your help.

/******
EDIT

******/

Well in case that someone has the same problem the solution is to serialize the arrays, insert them in a hidden field on a form and deserialize it on a PHP page

http://code.activestate.com/recipes/414334-pass-javascript-arrays-to-php/

  • 写回答

2条回答 默认 最新

  • dso0139 2011-07-27 02:40
    关注

    I am not sure if I get your question as you appear to have listed a PHP array there and not a Javascript one.

    Passing as one array from JS

    var pieces = [
        "base",
        "sleeves",
        "pocket"
    ];
    var colors = [
        "white",
        "red",
        "black"
    ];
    var send_array = {
        "colors": colors,
        "pieces": pieces
    };
    $.post('/my/url.php', send_array); //using jQuery for ajax request
    

    Extracting json array in PHP

    $send_array = json_decode($json, true);  
    

    Passing as one array from PHP to JS

    $pieces[0] = 'base';
    $pieces[1] = 'sleeves';
    $pieces[2] = 'pocket';
    
    $colors[0] = 'white';
    $colors[1] = 'red';
    $colors[2] = 'black';
    
    $send_array = array(
        'colors' => $colors,
        'pieces' => $pieces,
    );
    $json = json_encode($send_array);  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了