0

这是我的php代码,$contents必须在更改事件之后传递..

<?php

    if($flag == true){
        $contents = $store; // this is the array that needs to be passed
        $color = array();
        foreach($store as $item){
            $color[] = $item['color'];
        }
        $u_color = array();
        $u_color = array_unique($color);
        echo '<label>Available Colors:</label>
        <select id="color">
            <option>Select a color</option>';
        foreach($u_color as $item){
            echo '<option>'.$item.'</option>';
        }
        echo '</select>';
    }

    ?>

这是我的 jquery/ajax 代码,应该在更改事件之后触发

$(function () {
    $('#color').live('change', function () {
        var data = <? php echo json_encode($contents); ?> ;
        var the_array = $.parseJSON(data);
        $.ajax({
            url: 'wp-content/themes/twentyeleven-child/receiver.php',
            type: 'post',
            data: {
                data: the_array
            },
            datatype: 'json',
            success: function () {

            }
        });
    });
});

这是我的receiver.php

   <?php

print_r($_POST['data']);

?>

这是包含我的内容$contents

    Array
(
    [0] => Array
        (
            [size] => 2
            [price] => $59.00
            [color] => Black
        )

    [1] => Array
        (
            [size] => 4
            [price] => $59.00
            [color] => Black
        )

    [2] => Array
        (
            [size] => 6
            [price] => $59.00
            [color] => Black
        )

    [3] => Array
        (
            [size] => 8
            [price] => $59.00
            [color] => Black
        )

    [4] => Array
        (
            [size] => 10
            [price] => $59.00
            [color] => Black
        )

    [5] => Array
        (
            [size] => 12
            [price] => $59.00
            [color] => Black
        )

    [6] => Array
        (
            [size] => 14
            [price] => $59.00
            [color] => Black
        )

    [7] => Array
        (
            [size] => 16
            [price] => $59.00
            [color] => Black
        )

)
4

3 回答 3

1

尝试这个

 var the_array = $array.join() ;

将以逗号分隔的字符串形式加入数组,这可以传递给 Ajax 请求。

否则,您可以序列化您的数组并将数组对象也传递给您的请求,

 vat the_array = $array.serializeArray();
于 2012-10-16T22:37:49.703 回答
1

我在这里没有看到实际的 Ajax 请求……使用$.ajax……或$.post……或$.get

编辑和添加:

var data = '<?php echo json_encode($array);?>'; var the_array= $.parseJSON(data);

一个完整的例子:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<?php
  $array = array();
  $array['id'] = '2335';
  $array['data'] = 'data string';
?>

<script type="text/javascript">
$(function() {
  var data = '<?php echo json_encode($array);?>';
  var the_array = $.parseJSON(data);
  // keep in mind that the_array is Object not Array 
  // may be you should convert it to an Array
  // other way is to post 'data' adding it to array  var the_array = ('data': data);   
});
</script>
</body>
</html>
于 2012-10-16T22:42:57.943 回答
1

向页面发送ajax请求

你可以使用jQuery.ajax()通过 POST 将你的数组发送到服务器(你的代码缺少一些东西)

$.ajax({
  type: 'POST',
  url: 'receiver.php',
  data: { myarray: the_array },
  success: function(data){
    // executed on success
  },
  dataType: dataType
});

初始化渲染页面中的值

如果您想在呈现的 PHP 模板中将一些代码传递给 js,请使用json_encode()将您的 PHP 数组/对象“转换”为 JavaScript Object Notation 字符串:

<script>
var the_array = <?php print json_encode($the_array); ?>;
// ...
</script>

回复异步(ajax)请求

如果你想receiver.php返回一些 json 给调用者(例如成功函数),只需打印它

<?php

// .. do stuff here ..

header('Content-type: application/x-json');
print json_encode($the_array);

?>

然后,您将“自动”获得$the_array回调。datasuccess:

于 2012-10-16T22:46:37.647 回答