Page 1 of 1

sending array from PHP to JS

Posted: Wed Mar 17, 2004 10:41 pm
by dsdsdsdsd
hello;

Code: Select all

<?  // this script is in top.right where top -> frameset;
$problem=array('1','2','3','4','5','6','7');
print "  <script language="JavaScript" type="text/JavaScript"> ";
print "  top.left.JS_function($problem); ";   
print "  </script> ";    
?>

Code: Select all

<script type="JS"> // this script is in top.left
function JS_function(an_array)
  &#123; document.getElementById("a_div_tag").innerHTML="typeof =" + typeof(an_array); 
  &#125;
</script>
the response that I get from JS_function is 'typeof = function' instead of 'typeof = array';

any thoughts?

thanks
dsdsdsdsd
Asheville NC USA

Posted: Thu Mar 18, 2004 10:00 am
by scorphus
Happy to see you understand how PHP and JavaScript interact (?), but it is still a good idea to read the Frames, JavaScript, and PHP Overview tutorial.

This is the output of your script:

Code: Select all

&lt;script language="JavaScript" type="text/JavaScript"&gt;   top.left.JS_function(Array);   &lt;/script&gt;
'Array' is always echoed when you just echo or print an array in PHP. In this case 'Array' is taken as a function by JavaScript.

Take a look at this code:

Code: Select all

<?php
$problemArray = array('1','2','3','4','5','6','7');
$probArrString = implode(", ", $problemArray);
print "<script language="JavaScript" type="text/JavaScript">\n";
print "\ttop.left.JS_function(new Array($probArrString));\n";
print "</script>\n";
?>
and it's output:

Code: Select all

&lt;script language="JavaScript" type="text/JavaScript"&gt;
        top.left.JS_function(new Array(1, 2, 3, 4, 5, 6, 7));
&lt;/script&gt;
I think you can get the meaning of this by looking at these references:Hope it helps,
Scorphus.