sending array from PHP to JS

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dsdsdsdsd
Forum Commoner
Posts: 60
Joined: Fri Dec 05, 2003 3:10 pm

sending array from PHP to JS

Post 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
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
Post Reply