简体   繁体   中英

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

I cannot figure this out for the life of me.

I am trying to pass a single array to a php script processData.php that will then use that data in order to execute a perl script.

I then want to return the results of the perl script back to JQUERY for display.

It looks like when I try to display the data back, the data comes back as a NULL value.

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

So as you can see, to test, I am trying to pass an array to php, and then pass it back to JQUERY for display. I am getting a NULL value when I pass back, so I can only assume I am sending a NULL value.

If more information is needed I will gladly add it.

UPDATE

1) I have changed data: "input"+device, to data: {input: device},

I can now see the value via var_dump($data);

The problem I am having now is that the data being sent back to JQUERY is NULL.

SECOND UPDATE

1) Added: dataType: "json" 2) Changed GET to POST

New Error: 1) Parsererror 2) unexpected end of input 3) [Object][Object]

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

test this

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

In php

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

Ok I figured out what was wrong.....

PHP version 5.4 was not installed correctly

When I did a php -v it would tell me 5.4.7, however if I did an rpm -qa | grep php it would show me version 5.1.6.

I had to remove the rpm's for 5.6.1 and install the rpm's for 5.3 and it worked.

Thank you all for helping me in solving this!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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