如何使用ajax将PHP数组传递给另一个PHP页面

时间:2016-04-14 16:01:37

标签: javascript php jquery ajax

我一直在寻找这个答案而没有成功。 我有三个文件:index.php,actions.js和devices.php

我的index.php有这段代码:

allprojects {
    repositories {
        jcenter()
    }

    // Allow 400 errors.
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xmaxerrs" << "400"
        }
    }
}

此时我已经使用json_encode将$ areas变量传递给了JS,我的函数reloadDevices()和UpdateDevices()正确接收它,因为我之前已经检查过。

在我的actions.js文件中有以下代码:

<?php
        $file = "canvas/interactiveWorkstations/".$roomData['id'].".json";
        if(file_exists($file)){
            $map = "interactiveWorkstation";
            $lines = file($file);
            $nPolygon = $lines[count($lines) - 4];
            $counterPolygon = 0;
            $pos = 4;
            $areas = array();

            while($counterPolygon !== $nPolygon && $pos < count($lines)){
                $lines[$pos] = json_decode($lines[$pos], true);
                if($counterPolygon !== 0)
                    $lines[$pos] = array_diff_assoc($lines[$pos], $lines[$pos-9]);
                $coords = "";
                foreach($lines[$pos] as $line)
                    foreach($line as $k => $v)
                        if($k !== "color" && $v !== -1)
                            $coords .= $v . ", ";
                $coords = trim($coords, ', '); //Delete last space and last comma
                $lines[$pos-3] = trim($lines[$pos-3], '#');
                $areas[trim($lines[$pos-3])] = $coords;
                $counterPolygon++;
                $pos = $pos + 9;
            }
    ?>
        <script>
            var img = document.getElementsByClassName('connection')[0];
            img.setAttribute('usemap', '<?php echo "#".$map; ?>');
            img.insertAdjacentHTML('afterend', '<map name="<?php echo $map; ?>" id="<?php echo $map; ?>"></map>');
            var points = <?php echo json_encode($areas);?>;
        </script>
    <?php
        }
       if($bookingActive) {
echo '<script type="text/javascript">reloadDevices("'.$workstationName.'","'.$randomKey.'","'.$bookingData['ical_uid'].'",points); initCountdown('.$remainingTime.');</script>';
}

我对devices.php进行了ajax调用,但是当我想让$ _POST [&#39; points&#39;]变量为空时。 我的devices.php中使用此变量的代码部分:

function updateDevices(workstation,randomKey,z,points){
    var parameters = {
            "workstation" : workstation,
            "randomKey" : randomKey,
            "z" : z,
            "points" : points
    };
    $.ajax({
            data:  parameters,
            url:   'workstation/devices.php',
            type:  'post',
            success:  function (response) {
                    $("#devices").html(response);
            }
    });
}
function reloadDevices(workstation,randomKey,z,points) {
    updateDevices(workstation,randomKey,z, points);
    setInterval(function () { updateDevices(workstation,randomKey,z, points); }, 6000);
}

老实说,我无法看到错误。如果有人帮我欣赏它。 非常感谢。 问候。

0 个答案:

没有答案