-1

i have this in JAVASCRIPT , a.php -

function gettemplate(realnam) {

alert(realnam) }

i want to pass all the a[] array in func_a.php to the first file a.php.. to use the array there in javascript . how i do that? thanks a lot

EDIT-- ITS WORKS ! , if anyone need --

$a= json_encode($a);
    echo "<SCRIPT LANGUAGE='javascript'> gettemplate('$a');</SCRIPT>\n";

:)

Tamar Peled
  • 13
  • 1
  • 5
  • 1
    possible duplicate of [Best way to transfer an array between PHP and Javascript](http://stackoverflow.com/questions/393479/best-way-to-transfer-an-array-between-php-and-javascript) and several others... – T.J. Crowder Jun 06 '12 at 08:26
  • Why do you want it as an array? The Javascript is expecting an html fragment. – symcbean Jun 06 '12 at 08:36

3 Answers3

0

You will need to serialize and parse the Array, as XMLHttpRequests only can contain XML or raw text. The format of choice is JSON, which is broadly supported, including PHP and JavaScript.

Serverside you will use json_encode. Don't forget to serve the JSON with a valid MIME type. You also should encode your error messages to be valid JSON.

Clientside, i.e. in the callback function, you will use JSON.parse on the xmlhttp.responseText.

You also will find lots of information about this on the web, you only need to search.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

you can return (echo) in the func_a.php an json string http://de.php.net/manual/en/function.json-encode.php and parse it in javascript

echo json_encode($a);
David
  • 4,027
  • 10
  • 50
  • 102
  • Is it possible to do this: `var txt = check()); ?>;` where `check()` is a PHP function in the same file and it returns an array. Tried it, but now working. – Anish Nair Feb 05 '13 at 09:04
0

Read about JSON particulary json_encode. func_a.php must return something like this:

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

To get object in javascript use this:

var myResponseObject = JSON.parse(xmlhttp.responseText);
Ruben Nagoga
  • 2,178
  • 1
  • 19
  • 30