繁体   English   中英

如何从JQUERY将数组传递给PHP,然后从PHP将数组发送回JQUERY

[英]How do you pass an array to PHP from JQUERY and then send an array back to JQUERY from PHP

我无法想象我的生活。

我试图将一个数组传递给php脚本processData.php,然后将使用该数据执行perl脚本。

然后我想将perl脚本的结果返回给JQUERY进行显示。

看起来当我尝试重新显示数据时,数据会以NULL值的形式返回。

JQUERY PAGE:
$('#numDevices').click(function(event) {
        event.preventDefault();
        var test = [];
        var $number = $('#element');
        var value = $number.val();
        var container = $('#main');
        $('.device').remove();
        $('#send').remove();
        for(var i=0; i<value; i++) {
             container.append('<div class="device"><fieldset><legend>Device' + i + '</legend><textarea class="input"  rows="10">Please enter a hostname, followed by the corresponding links.</textarea></fieldset></div>');

        }
        container.append('<input type="submit" id="send" name="send" value="submit"/>');
         $('#send').click(function(event) {
             event.preventDefault();

             var device = new Array();
             $('textarea').each(function(index, area) {
                 var $area = $(area).val();
                 device.push($area);
             });

             $.ajax({
                 type: "GET",
                 url: "processData.php",
                 data: "input"+device,
                 cache: false,

                 success: function(data) {
                     alert(data);
                 },
                 error: function() {
                     alert("FAILED");
                 }
             });
         });
    });

.....

 PHP PAGE:
<?php
$data =$_GET['input'];
echo json_encode($data);
?>

所以你可以看到,为了测试,我试图将一个数组传递给php,然后将它传递给JQUERY进行显示。 当我传回时,我得到一个NULL值,所以我只能假设我发送一个NULL值。

如果需要更多信息,我很乐意添加它。

UPDATE

1)我更改了data: "input"+device, data: {input: device},

我现在可以通过var_dump($ data)看到值;

我现在遇到的问题是发送回JQUERY的数据是NULL。

第二次更新

1)添加: dataType: "json" 2)将GET更改为POST

新错误:1)Parsererror 2)意外结束输入3)[对象] [对象]

var device = $.map($('textarea'), function(el, i) { return el.value; });

$.ajax({
     type: "GET",
     url: "processData.php",
     dataType: 'JSON',
     data: {input : device.join('$$')},
     cache: false
}).done(function(json) {
     console.log(json);
});

PHP

<?php
    $data = $_GET['input'];
    echo json_encode( explode('$$', $data) );
?>

测试一下

$('#numDevices').click(function(event) {
        event.preventDefault();
        var test = [];
        var $number = $('#element');
        var value = $number.val();
        var container = $('#main');
        $('.device').remove();
        $('#send').remove();
        for(var i=0; i<value; i++) {
             container.append('<div class="device"><fieldset><legend>Device' + i + '</legend><textarea class="input"  rows="10">Please enter a hostname, followed by the corresponding links.</textarea></fieldset></div>');

        }
        container.append('<input type="submit" id="send" name="send" value="submit"/>');
         $('#send').click(function(event) {
             event.preventDefault();

             var device = new Array();
             $('textarea').each(function(index, area) {
                 var $area = $(area).val();
                 device.push($area);
             });

             $.ajax({
                 type: "POST",
                 url: "processData.php",
                 data: {input : device},
                 cache: false,
                 dataType: 'json',
                 success: function(data) {
                     alert(data[0]);  
                     alert(data[1]);  
                 },
                 error: function() {
                     alert("FAILED");
                 }
             });
         });
    });

在PHP中

<?php
$data =$_POST['input'];
echo json_encode($data);
?>

好的我弄清楚出了什么问题......

PHP版本5.4未正确安装

当我做了一个php -v它会告诉我5.4.7,但是如果我做了rpm -qa | grep php它会告诉我5.1.6版本。

我必须删除rpm的5.6.1并安装rpm的5.3并且它工作。

谢谢大家帮我解决这个问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM