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.

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

2 Answers 2

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

1 Comment

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) { } }
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;

1 Comment

In the complet function, how do I load the berechnungseingabe.php page because there I want to use the array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.