通过POST将数组从JQuery传递到PHP

时间:2013-09-21 18:33:26

标签: php jquery

我有两个变量和一个我试图传递给PHP页面的数组。不幸的是,虽然我做了一些搜索,但我无法成功通过我的数组。变量工作正常。有什么帮助吗?

继承我的JQuery代码:

$('#submit').click(function() {
        $.post('process.php', {primary: $prim, secondary: $seco, not: $not}, 
        function(data) {
            //alert("Data Loaded: " + data);
            $("#error").html(data);
        });
});

,其中

$not = {1,2,3};

process.php

<?php
    require("common.php");
    $primary = $_POST['primary'];
    $secondary = $_POST['secondary'];
    $not = $_POST['not'];
    //echo("primary: "+$primary+", secondary: "+$secondary+", not: "+$not);
    echo($primary);
?>

错误消息

Notice: Undefined index: not in C:\wamp\www\process.php on line 5

刚刚意识到它可能是因为`not`是php

中的关键字

不,这不是因为那个

1 个答案:

答案 0 :(得分:0)

在您的JQuery中使用join(“,”)

$('#submit').click(function() {
        $.post('process.php', {primary: $prim, secondary: $seco, not: $not.join(",")}, 
        function(data) {
            //alert("Data Loaded: " + data);
            $("#error").html(data);
        });
});

在PHP中使用explode()函数将字符串转换为数组

<?php
    require("common.php");
    $primary = $_POST['primary'];
    $secondary = $_POST['secondary'];
    $not =  explode(',', $_POST['not']) ;
    //echo("primary: "+$primary+", secondary: "+$secondary+", not: "+$not);
    echo($primary);
?>