1

我现在正在使用 jQuery .post 编写一个表单,负责处理的文件具有以下代码:

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/

4

3 回答 3

4

我认为你应该改变这一行:

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

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

请注意,我将第二个参数从对象文字更改为(url 编码的)字符串。这会将表单中的每个变量作为单独的变量发布(就像直接发布一样)。$_POST在服务器端,每个变量都应该在数组中单独可用。

于 2012-04-17T11:48:47.073 回答
1

试试parse_str ()

就像是:

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

$capacity = $output['capacity'];
$bookingdate = $output['bookingdate'];
$grade = $output['grade'];
于 2012-04-17T11:38:19.690 回答
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

于 2012-04-17T11:38:42.063 回答