Skip to content Skip to sidebar Skip to footer

Send Arrays Of Data From Php To Javascript

My jphp.php file contains the following :

Solution 1:

Simple specific exmple using jquery:

The javascript page:

$.ajax({
  url: 'url/of/page.php'type: 'post', // post or get methoddata: {}, // if you need to pass post/get parameterds you can encode them here in JSON formatdataType: 'json', // the data type you want returned... we will use jsonsuccess: function(responseData) {
    var edge_number = responseData.edge_number;
    var vertex_a= responseData.vertex_a;
    var rec_array = responseData;
  }
});

In your php:

$send_array = array(
  'edge_number' => array('a','b'),
  'vertex_a' => array('c','d')
); 

header('Content-type: application/json');
echo json_encode($send_array);

Solution 2:

The JavaScript calls the PHP via AJAX, and then when it gets the respone it uses JSON.parse() to turn the JSON string into JavaScript objects.

Solution 3:

On the client-side I would recommend using jQuery and it's $.parseJSON() function. You can do the AJAX call using $.get(), $.post() or $.ajax(). See the documentation for their usage.

On the server-side, encode your array with PHP native json_encode() function. Then set the correct HTTP header (!!!)

header('Content-type: application/json');

and echo the JSON encoded data =]

Post a Comment for "Send Arrays Of Data From Php To Javascript"