0

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]

1
  • Don't "assume". There are plenty of utilities out there that help you check what's really going on (like Fiddler or Chrome Developer Tools) Commented Apr 28, 2013 at 22:09

3 Answers 3

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) );
?>
Sign up to request clarification or add additional context in comments.

13 Comments

ahhh ok I think I may be getting closer. After doing a var_dump I can see the value is no longer null and is array(1){ [0]=> string(61) "The correct value" }
It seems like the problem I have now is I am guessing this is incorrect? echo json_encode($data);
So what does the alert say ?
alert is empty. No value at all.
Then it does'nt fail, you should be using the console to see what you're getting back, and not alerts. Drop the json_encode and see what is returned ?
|
0

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

7 Comments

And that would show [object, object], as alerts can't show objects ?
when i use var results = JSON.parse(data); alert(results) it comes back and says "unexpected end of input" error
try this little cute function instead of alert, function dump(obj) { var out = ''; for (var i in obj) { out += i + ": " + obj[i] + "\n"; } alert(out); }
also use "POST" instead of "get" it is safer
also try adding "json" after data :{input : device},
|
0

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!!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.