0

My jphp.php file contains the following :

<?php

$send_array = array();
$edge_number = array('a','b');

$vertex_a = array('c','d');

$send_array[0] = $edge_number;
$send_array[1] = $vertex_a;

echo json_encode($send_array);

?>

and my javascript file contains the following:

<html>
<head>
<script language="javascript">
function postRequest(strURL)
{
    var xmlHttp;
    if(window.XMLHttpRequest)
    { // For Mozilla, Safari, ...
        var xmlHttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    { // For Internet Explorer
        var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttp.open('GET', 'jphp.php', true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4)
        {
    var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );       updatepage(xmlHttp.responseText);
        }
    }
    xmlHttp.send('jphp.php');
}

function updatepage(str)
{
    document.write(str);
}



var vertex_a = new Array();
var edge_number = new Array();
var rec_array = new Array();
rec_array = {"edge_number", "vertex_a"};
//rec_array[1] = names;
for(var i=0;i<1;i++)
{
    document.write(rec_array[i]);
}
$.ajax({
  url: 'jphp.php'
  type: 'post', // post or get method
  data: {}, // if you need to pass post/get parameterds you can encode them here in JSON format
  dataType: 'json', // the data type you want returned... we will use json
  success: function(responseData) {
    alert('edge_number='+responseData[0].join(','));
    alert('vertex_a='+responseData[1].join(','));
  }
});

I have encode the data data in php .... now i want to send those two arrays of data to javascript..... i don't know the proper commands to use. I am getting confused on googling.

Please help .

prodigitalson
  • 60,050
  • 10
  • 100
  • 114

3 Answers3

0

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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • can u please tell me the specific commands too .... i have been trying this for 2-3 hours .... please .... specially for the javascript part ! –  Nov 27 '10 at 08:26
  • can u tell me one more thing : how can I build 2 arrays inside a single array .... the elements of the individual arrays have to be received through php . –  Nov 27 '10 at 08:31
0

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 =]

TomWilde
  • 248
  • 1
  • 2
  • 7
  • can u tell me one more thing : how can I build 2 arrays inside a single array .... the elements of the individual arrays have to be received through php –  Nov 27 '10 at 08:32
  • ofc, `$container=array($firstarray,$secondarray);` no matter how many dimensions ("depth-levels") your array has. `json_encode` will do the hard work for you. – TomWilde Nov 27 '10 at 08:37
0

Simple specific exmple using jquery:

The javascript page:

$.ajax({
  url: 'url/of/page.php'
  type: 'post', // post or get method
  data: {}, // if you need to pass post/get parameterds you can encode them here in JSON format
  dataType: 'json', // the data type you want returned... we will use json
  success: 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);
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • thnx prodigy yet again ! Let me try this. –  Nov 27 '10 at 08:44
  • i just adjusted to use named keys instead of numeric indexes. it would be much eaiser if you gave a discription of what format you actually need to consume thes in in js.. because you can format it that way in php directly and send it back as json. – prodigitalson Nov 27 '10 at 08:46
  • my php script was the same as yours ....for the javascript ....wait ....please ...m running it –  Nov 27 '10 at 08:47
  • i am actually facing problems in decrypting the encoded part .... on the client side –  Nov 27 '10 at 08:51
  • actually I think m AJAX part is not working ...can u also tell me the problem in the ajax part ..., I want my data values of the 2 arrays to be printed on the screen . –  Nov 27 '10 at 08:56
  • how do you want them printed?... also if youre goign to use my code you need to include `jQuery` in a script tag... http://jquery.com/ – prodigitalson Nov 27 '10 at 09:00
  • what should the command be like ... should i use this src = "jQuery.js" in the script tag. –  Nov 27 '10 at 09:04
  • can u also please check my updatepage() function .... i am unable to understand $.ajax{......} i think if dat could be sorted out ... den i'dbe done ! –  Nov 27 '10 at 09:06
  • Also is there any diffeerence between responseText and responseData ? –  Nov 27 '10 at 09:08
  • `responseData` will always be the format you specified in `dataType`, in this case a native javascript hash - ie. `parseJSON`/`eval` has already been run on the response returned. read the docs on jQuery's ajax features for more detail: http://api.jquery.com/category/ajax/ – prodigitalson Nov 27 '10 at 17:18