Forum Moderators: coopster

Message Too Old, No Replies

Passing array values from PHP to javascript

         

Ananthi

3:15 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



How can i pass an array called $n[] to javascript

Little_G

3:50 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



Hi,

Something like:

<?php
$n = array('test','test2');
echo '<script type="text/javascript">';
echo 'var n = new Array("', join($n,'","'), '");';
echo '</script>';
?>

Andrew

Ananthi

4:11 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



Thanks a lot LITTLE_G

your code works.

Ananthi

5:08 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



hi Andrew

Now I get into a new problem.I just slightly modify your code.

Please go thro' this code..

<?php
$conn=mysql_connect('localhost','root','');
mysql_select_db('univ',$conn);
$query="select * from enews";
$result=mysql_query($query,$conn);
$rows=mysql_num_rows($result);
$n = array();
for($i=0;$i<$rows;$i++)
{
$data=mysql_fetch_row($result);
$n[] = $data[0];
}
echo '<script type="text/javascript">';
echo 'var n = new Array("', join($n,'","'), '");';
echo '</script>';
?>
<script type="text/javascript">
var a=n.length
alert(n);
</script> // code is perfect upto this point.

Now I want to store this js array into a string as :

var str = "n[0]+ imagefile + n[1] + imagefile +.........+end of array"

How can I do this?

Please solve my problem

Thanks in advance

Ananthi.

Little_G

7:41 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



Hi,

Try this:
<script type="text/javascript">
var buffer = "";
for(var i in n){
buffer += "n[" + i + "]= " + n[i] + "\n";
}
alert(buffer);
</script>

Andrew

Ananthi

7:42 am on Jan 18, 2007 (gmt 0)

10+ Year Member



Thanks a lot for your response.

My problem was solved.