Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have 2 pages.

berechnungseingabe.php

where I generate a Multidimensional Javascript Array

Array[19]
3: Array[0]
7: Array[0]
11: Array[0]
      Anzahl: "1"
      Driving: 2380
      Index: "13"
      Latitude: 48.0078390267396
      Longitude: 16.224982738494873
      Walking: 1647
13: Array[0]
14: Array[0]
...
x: Array[0]

when i chlick on the Button

<button class="ui-btn ui-corner-all ui-btn-icon-left ui-shadow ui-icon-action" onclick="berechnenDerWege()">Berechnen</button>   

I want to send the array to the

berechnungsergebniss.php

that I can use it as php array on this page.

Is there a way with jQuery, JavaScript,PHP?

JSON.stringify(array);

does not work because I get:

[[],null,[],null,null,null,null,null,null,null,null,null,null,null,[]]

as result.

share|improve this question

marked as duplicate by Jack Jul 1 at 8:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
ever heard of $.ajax –  user3778553 Jul 1 at 7:50
    
Yes, but I am not sure how to do it with $.ajak. can you post me a code example? pleas –  Phil Jul 1 at 7:53
add comment

2 Answers 2

up vote 1 down vote accepted

Of course, yes. JSON exists to share objects easly.

In your function call berechnenDerWege() you must encode your array to JSON with javascript.

JSON.stringify(myArray)

It will output you a string that you can pass to your PHP script through Ajax or a direct link. It depends of your implementation.

Example without Ajax :

function berechnenDerWege() {
   var myArray = [1,2,3,[5,6,7]];
   document.location.href='berechnungsergebniss.php?data='+JSON.stringify(myArray);
}

To decode data with PHP use json_decode().

berechnungsergebniss.php :

<?php $data = json_decode($_GET['data']); 
var_dump($data);
?>
share|improve this answer
    
I get this as result array(12) { [0]=> array(0) { } [1]=> NULL [2]=> NULL [3]=> NULL [4]=> array(0) { } [5]=> NULL [6]=> NULL [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> array(0) { } } –  Phil Jul 1 at 9:49
add comment

You can use jQuery AJAX:

$.ajax({
    type : 'post',
    url : 'path/to/berechnungseingabe.php',
    data : {array : JSON.stringify(yourArray)},
    complete : function(data){
        console.log(data);
    }
});

And on server side:

$yourArray = json_decode($_POST['array']);
echo $yourArray;
share|improve this answer
    
In the complet function, how do I load the berechnungseingabe.php page because there I want to use the array. –  Phil Jul 1 at 10:11
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.