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

时间:2013-04-28 22:05:55

标签: php jquery

我无法想象我的生活。

我正在尝试将单个数组传递给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值。

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

更新

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

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

查看值

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

第二次更新

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

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

3 个答案:

答案 0 :(得分:2)

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) );
?>

答案 1 :(得分:0)

测试这个

$('#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);
?>

答案 2 :(得分:0)

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

PHP版本5.4未正确安装

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

我必须删除5.6的rpm并安装rpm的5.3并且它有效。

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