0

I there, I try to use a php-array with ajax to POST it to another php-file. I thought I could do it this way:

<?php
$test_array[] = "dum";
$test_array[] = "dim";
$test_array[] = "dam";
$test_array[] = "dom";
$test_array[] = "düm";
?>
<script type="text/javascript">

    $.ajax({ url: 'include.php',
             type: 'POST',
             data: { my_array : <?php echo json_encode($test_array); ?> 
                   },
        success: function(returnData)
             alert(returnData);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
             alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
    }

        }); 
}
</script>

with include.php looking like this:

echo count($_POST['my_array']);
echo json_decode($_POST['my_array']);
echo $_POST['my_array'];

The problem is, that no array is going to incude.php The echos give "1düm", means, the length of the $_POST-var is 1 (while the original array is 5), and its entry is only "düm" (which is the last entry of the original array). It looks like this:

count:  1
json_decode:
$_POST:  düm

So, it seams that not the complete array, but only the last entry is POSTed...

How can I get the whole array $test_array to include.php via that ajax-call?

thx in advance...

9
  • Try to change your array to $test_array['a'] = "dum"; $test_array['b'] = "dim"; and so on. Maybe that will help. Commented Mar 15, 2014 at 22:24
  • why should that make any difference, Gil? Commented Mar 15, 2014 at 22:28
  • Maybe it won't, but when you pour php into json and back, something might get lost in the process. Maybe the php gets the whole array but every value override the previous one so you're left with the last value inserted. Commented Mar 15, 2014 at 22:35
  • this is not the case... Commented Mar 15, 2014 at 22:57
  • Well, what do you get when you use var_dump(json_decode($_POST['my_array']))? Commented Mar 15, 2014 at 23:04

3 Answers 3

1

try this, make a javascript Array and pass it to ajax function

var myArr=["<?php print implode('","', $test_array)?>"];
$.ajax({ url: 'include.php',
             type: 'POST',
             data: { my_array : myArr 
                   },
        success: function(returnData)
             alert(returnData);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
             alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
    }

        }); 

and no need to json_decode in include.php anymore

Sign up to request clarification or add additional context in comments.

2 Comments

with this, array-length is still 1, and content is all entries after another... thx for your answer, but this is not what I suspect to get...
thx again, but with your new code I get the same as with my code: array has length = 1 and entry is only "düm" (the last entry of the original array).
1
<?php
$test_array[] = "dum";
$test_array[] = "dim";
$test_array[] = "dam";
$test_array[] = "dom";
$test_array[] = "düm";
?>
<script type="text/javascript">
var stuff = <?php echo json_encode($test_array);?>;
        var jsonString = JSON.stringify(stuff);
    $.ajax({ url: 'include.php',
             type: 'POST',
             data: {data : jsonString},
        success: function(returnData)
             alert(returnData);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
             alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
    }

        }); 
}
</script>

Comments

0

in ajax, change line:

my_array : <?php echo json_encode($test_array); ?> 

into

my_array : JSON.stringify(<?php echo json_encode($test_array); ?>)

So that it looks like this:

<?php
$test_array[] = "dum";
$test_array[] = "dim";
$test_array[] = "dam";
$test_array[] = "dom";
$test_array[] = "düm";
?>
<script type="text/javascript">

    $.ajax({ url: 'include.php',
             type: 'POST',
             data: { my_array : JSON.stringify(<?php echo json_encode($test_array); ?>) 
                   },
        success: function(returnData)
             alert(returnData);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
             alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
    }

        }); 
}
</script>

In include.php use

$my_array = json_decode($_POST['my_array'])

to get the array back.

However, this won't work with multidimensional arrays, for example, if the array looks like this:

$test_barry[1][1] = "1a";
$test_barry[1][2] = "1b";
$test_barry[1][3] = "1c";
$test_barry[2][1] = "2a";
$test_barry[2][2] = "2b";
$test_barry[2][3] = "2c";
$test_barry[3][1] = "3a";
$test_barry[3][2] = "3b";
$test_barry[3][3] = "3c";

you need to fetch back the multidimensional array in input.php with

$my_array = json_decode($_POST['my_array'],TRUE) // add TRUE

1 Comment

the clue was to use the php-array within the ajax-call as: JSON.stringify(<?php echo json_encode($test_array); ?>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.