0

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.

Phil
  • 2,396
  • 2
  • 18
  • 15
  • 2
    ever heard of [**`$.ajax`**](http://api.jquery.com/jquery.ajax/) –  Jul 01 '14 at 07:50
  • Yes, but I am not sure how to do it with $.ajak. can you post me a code example? pleas – Phil Jul 01 '14 at 07:53

2 Answers2

1

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);
?>
krishna
  • 4,069
  • 2
  • 29
  • 56
Kevin Labécot
  • 2,005
  • 13
  • 25
  • 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 01 '14 at 09:49
1

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;
Lewis
  • 14,132
  • 12
  • 66
  • 87
  • In the complet function, how do I load the berechnungseingabe.php page because there I want to use the array. – Phil Jul 01 '14 at 10:11