dongpinyao2203 2011-07-27 10: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 10: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 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线