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 .

share|improve this question

80% accept rate
feedback

3 Answers

up vote 1 down vote accepted

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);
share|improve this answer
thnx prodigy yet again ! Let me try this. – abcdef Nov 27 '10 at 8: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 8:46
my php script was the same as yours ....for the javascript ....wait ....please ...m running it – abcdef Nov 27 '10 at 8:47
i have just added my whole of javascript code in the edited code .... – abcdef Nov 27 '10 at 8:49
i am actually facing problems in decrypting the encoded part .... on the client side – abcdef Nov 27 '10 at 8:51
show 6 more comments
feedback

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.

share|improve this answer
can u please tell me the specific commands too .... i have been trying this for 2-3 hours .... please .... specially for the javascript part ! – abcdef Nov 27 '10 at 8: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 . – abcdef Nov 27 '10 at 8:31
Just encode them together. – Ignacio Vazquez-Abrams Nov 27 '10 at 8:38
feedback

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

share|improve this answer
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 – abcdef Nov 27 '10 at 8: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 8:37
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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