2012-04-17 65 views
0

我编码一种形式,现在使用jQuery。员额和负责处理该文件具有代码:开口柱阵列为多个变量

print_r($_POST); 

它返回以下动态输出:

Array ([data] => capacity=50-1000+people&bookingdate=17%2F04%2F2012&grade=1+star) 

我想这个数组分成三个变量即能力预订日期,但真的不知道 如何。任何想法如何?我试过使用echo $ _POST [“capacity”];但它不起作用。

在此先感谢!

编辑

这是jQuery的我使用:

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#postData").click(function() { 
     $("#last-step").hide(600); 


     $.post('resources/process2.php', { data: $("#task5_booking").serialize() }, function(data) { 
      $("#result").html(data); 
     }); 


     return false; 
    }); 
}); 
</script> 

这正与以下形式:

http://jsfiddle.net/xSkgH/93/

+2

看起来你是双重编码数据的URL。虽然很容易从字面上回答你的问题,但真正的问题似乎在于你如何从jQuery端发布数据。 – deceze 2012-04-17 11:37:41

+0

尝试爆炸到您的阵列 – 2012-04-17 11:37:48

+0

我想这将是更好的编码您的数据到js js中,然后在服务器端解码 – k102 2012-04-17 11:38:57

回答

3

我想你应该改变这一行:

$.post('resources/process2.php', { data: $("#task5_booking").serialize() }, function(data) { 

$.post('resources/process2.php', $("#task5_booking").serialize(), function(data) { 

请注意,我将第二个参数从对象文字更改为(url编码)字符串。这将您的表单中的每个变量作为一个单独的变量进行发布(就像它是直接发布一样)。在服务器端,每个变量应在$_POST阵列内单独提供。

1

parse_str()

一去

类似于:

parse_str($_POST['data'] , $output); 

$capacity = $output['capacity']; 
$bookingdate = $output['bookingdate']; 
$grade = $output['grade']; 
1

您必须为此使用爆炸。

$data = array(); // A array to store the extracted value 
$temp = explode("&", $data); // First of all explode by "&" to get the each piece 
foreach($temp as $tval) { 
    $t = explode('=', $tval); // Next explode by "=" to get the index and value 
    $data[$t[0]] = $t[1]; //Add them to the array 
} 

另一种方法是使用parse_str()

$data = array(); 
parse_str($_POST['data'], $data); 

这一切后的值将被映射到$data