这是我的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
)
)