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 .

3 Answers 3

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

10 Comments

thnx prodigy yet again ! Let me try this.
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.
my php script was the same as yours ....for the javascript ....wait ....please ...m running it
i am actually facing problems in decrypting the encoded part .... on the client side
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 .
|
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.

2 Comments

can u please tell me the specific commands too .... i have been trying this for 2-3 hours .... please .... specially for the javascript part !
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 .
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 =]

2 Comments

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
ofc, $container=array($firstarray,$secondarray); no matter how many dimensions ("depth-levels") your array has. json_encode will do the hard work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.